Parse JSON with arduino to turn on LED

I'm aware of a few JSON parsing libraries for Arduino.

  • aJSON
  • Arduino JSON Parser
  • JSON Arduino

I've never used any of these but I did do some simple JSON parsing in a project I'm working on so I'll show you how I did that.

NOTE: I'm reading serial data using the software serial library. You'll need to change this code to work for you. This will only work on very simple JSON strings. It's very limited but if that's all you're parsing then it'll work.

Example of JSON response from server:


First, only read data between curly braces.

String response = "";
bool begin = false;
while (SIM900.available() || !begin) {

    char in =;

    if (in == '{') {
        begin = true;

    if (begin) response += (in);

    if (in == '}') {


This code reads data one byte at a time and once it gets an open brace, it starts saving it into response. When it gets a closing brace, it ends it. So here's a clear limitation, you can only have one set of opening/closing braces in your string.

Once I have the string, I use indexOf and substring to extract relevant information:

start = response.indexOf("id\":\"") + 5;
end = start + 5;
nodeId = response.substring(start, end);

This code sets start to the beginning of id":" + 5 characters in the string. It's +5 because that's how long id":" is. So start points to TEST1 in the JSON string. In my system, the ID is always going to be 5 characters long so end is start + 5. I then use substring to extract that.

Again, before anyone starts down voting me for this horrible solution: if you know exactly what you're working with, and understand the limitations of this code, then this is not a bad solution. It's a solution that gets the job done.

I have programmed a class that will receive char by char the JSON document. It only will store in memory a few bytes for known JSON structure using a state machine and the results you need. So you can query the class for the results you want and will process the JSON.

Its ideal for your purpose. I have used for connecting to a weather service that returns a json:

static const char* queries[] = { "list.0.deg", ""};
StreamJsonReader jsonreader(queries, 2); // 2 queries    
while(char c  =  read()){

cout << jsonreader.results[0] << endl;
cout << jsonreader.results[1] << endl;

Check this blog post I just wrote:

Source code is here in Biicode.

I wrote a parser for the PIC that has very low ram use because it works directly with char pointers and does not build a tree structure. If you ask to get item N of a list, it gives you an actual pointer into the direct JSON file right at the start of item N of the list. Then you can ask what type of object the pointer represents, etc. It's buggy and doesn't support floats and I think has a few other limitations(twas a long time ago) but it's all on github: You'll need to look in libfortune which is part of the C version.