Parsing JSON in Arduino

JSON

I guess most of you would by now, know what is JSON. It is a data format to represent structured data like XML, but is very small and has native support in JavaScript. Libraries are available in various languages for parsing JSON.

But I was stunned when I read about aJson Arduino library. The author of this library has ported JSON parsing to Arduino. JSON parsing in Arduino, opens up lot of opportunities and I immediately wanted to test it.

Parsing JSON (decode)

JSON decoding or parsing means, converting JSON string into objects or data structures so that we can retrieve all or selected information from it.

aJSON library provides the parse method which allows you to parse or decode JSON strings.

Consider a JSON structure like the following.

To parse it, all you need is just this piece of code.

In addition to parsing simple structure like above, the library is also capable of parsing complex or nested JSON structure like the following.

Building JSON (encode)

JSON encoding is the reverse process of JSON decoding. Here you create JSON strings from objects or data structures.

aJSON library provides addItemToObject method for encoding or creating JSON strings.

To build the same JSON string that we used above, you just have to do the following.

Full working example

I have created a complete sketch that parses a complex nested JSON string. This JSON string is actually a response of a YQL call. You can find this sketch at github. I am also going to contact the author of the library to see if this can be added to the examples sketches of the library.

Update: This sketch is now part of examples that ships with the aJson library.

Additional features supported by aJson

In addition to the above features aJson also supports the following advanced features.

Manipulating JSON Objects

After you create a JSON object, either by parsing a JSON string or by creating a JSON object, the library allows you to manipulate these JSON objects.

Filtering while parsing

You can pass an optional filter parameter to the parse method, which ignores the list of keys present in the filter array. This feature is very useful, if you have to parse a huge JSON object and you have less memory to spare.

Parsing streams

In addition to parsing strings, the library also has the ability to parse streams directly without storing them first. This feature is again very useful, if you are running out of space. I have not managed to get this feature to work yet. Will write about it in detail once I get this to work.

Let me know if you have done something cool with this library. Happy hacking 🙂

14 thoughts on “Parsing JSON in Arduino

  1. Pingback: Accessing YQL from Arduino | Hardware Fun

  2. rinnamon

    I tried filtering while parsing according to the readme on github but got some errors while compiling. Have you succesfully gotten the filter or the stream parsing to work?

    Reply
  3. Tim Johnson

    I would love to use this code, it would definitely save me a lot of time, but it’s not compiling. I’ve installed the library and the example “Json_example” works great. Just what I want!

    However, when I compile my code it is not working. I’m getting:

    WifiBotControlCC3000.ino: In function ‘void loop()’:
    WifiBotControlCC3000:196: error: no matching function for call to ‘aJsonClass::parse(String&)’
    C:\Users\tjohnson\Documents\Arduino\libraries\aJson/aJSON.h:179: note: candidates are: aJsonObject* aJsonClass::parse(aJsonStream*)
    C:\Users\tjohnson\Documents\Arduino\libraries\aJson/aJSON.h:180: note: aJsonObject* aJsonClass::parse(aJsonStream*, char**)
    C:\Users\tjohnson\Documents\Arduino\libraries\aJson/aJSON.h:181: note: aJsonObject* aJsonClass::parse(char*)
    WifiBotControlCC3000:198: error: invalid operands of types ‘const char [14]’ and ‘char*’ to binary ‘operator+’
    WifiBotControlCC3000:200: error: invalid operands of types ‘const char [14]’ and ‘char*’ to binary ‘operator+’

    Any help would be greatly appreciated. Thanks!

    Reply
  4. Alex

    Could you please also explain how to iterate through multiple elements? What you have there is great but if one doesn’t know the values beforehand or has many elements this would be very helpful…

    NO LUCK:
    aJsonObject* query = query.next();
    Serial.println(query->valuestring);

    Any ideas?

    Reply
      1. Alex

        Problem solved: For anybody interested….
        Iterate through an array:
        aJsonObject* query = aJson.getObjectItem(root, “online presence”);
        if(query != NULL) {
        aJsonObject* childObject = query->child;
        while (childObject)
        {
        Serial.println(childObject->valuestring);
        childObject = childObject->next;
        }
        }
        Cheers to all!

        Reply
  5. Hrishi

    Hi,

    I’m having trouble parsing a multi-level JSON string with the aJSON library. I eventually see the “Parsin failed” message on the serial console.

    Here is the code inside void setup():

    char* value;

    aJsonObject* root = aJson.parse(deltaRecvd);

    if (root != NULL)
    {
    Serial.println(“Parsed root (level 0)”);
    aJsonObject* state = aJson.getObjectItem(root, “state”);
    if (state != NULL)
    {
    Serial.println(“Parsed state (level 1)”);
    aJsonObject* deviceState = aJson.getObjectItem(state, “deviceState”);
    if (deviceState != NULL)
    {
    Serial.println(“Parsed deviceState (level 2)”);
    value = deviceState->valuestring;
    }
    }
    }

    if (value) {
    Serial.print(“Value: “); Serial.println(value);
    } else {
    Serial.println(“Parsing failed”);
    }

    And here is the JSON string that I’m trying to parse. I’m trying to get the deviceState value.

    {
    “version”: 2,
    “timestamp”: 1470398018,
    “state”: {
    “deviceState”: 21
    },
    “metadata”: {
    “deviceState”: {
    “timestamp”: 1470398020
    }
    }
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *