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.
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 🙂