Arduino JSON

Hayden Dekker
3 min readFeb 23, 2020

Exploring the popular ArduinoJson library for embedded systems

JSON is a popular interchange format that is compatible with nearly all languages. As an example, I’m using JSON as the messaging format between Java based Cloud Services, MQTT brokers and C based IOT endpoints. My endpoints are low memory, low power processors which means I aim to keep the data transfer minimal so in my applications I’m looking for a compact JSON library.

Ahead of the benefit of programming compatibility, is the benefit in using JSON for communications at the Human Machine Interface. It really, really helps with the reading and writing of configuration and system state. The increase of memory usage to strengthen communication is worthwhile tradeoff.

Within a program, native objects are far smaller and faster than JSON objects so for this reason it’s not great passing JSON objects around to various parts of the application. The Arduino JSON library fits this need. In fact, there is only one way to use ArduinoJson. ArduinoJson returns references to all objects, no pointers, an you can’t new up any objects. This means it’s intended to be used as a service and not an object. You can’t pass the JSON objects around to different components in your program. You need to take the input Json strings and use ArduinoJson to inspect and convert the those strings to the structures or classes that are then be passed around to the various components of your program.

In the rest of this article I explore some functions of the library.

Setup

I’ve gone straight to the source and cloned this repository to a local repo. In eclipse I’ve setup a new project and included the src folder in the project settings. The setup is as simple as that.

Compatibility

The Arduino library has a certain way of doing things. I don’t want the full suite just to use the JSON library and luckily I don’t need to. The JSON library makes some assessments of the environment to see if the Arduino modules are present and if they aren’t, it’ll revert to standard C. This can be seen in the treatment of strings. The#define ARDUINOJSON_ENABLE_STD_STRING 1 allows use of the std::string library of Arduino’s String. Perfect.

Using the library

From the website serialize and deserialize are the two key functions to convert and revert plain text. The simply take memory in and produce object that can be extracted to other parts in the program.

Once deserialized, extracting an object is very simple and straightforward.

The tricky bit to me is in handling the memory. I caused a fault early on which prompted me to investigate this.

Testing

I’ve setup a git. For the test I’ve created two test arrays and pass them in and out of a few functions to figure out exactly how the library uses my heap and memory. It’s actually very smart and efficient.

TL;DR - In Summary

The library architecture forces you to use it as a service. You must serialise to structs that can then be managed by your application. JSON on embedded systems make sense only at the network interface. Always open for discussion though.

--

--

Hayden Dekker

Systems Engineer building a new CBTC train line for Melbourne Australia. Weekends spent head in the cloud programming or embedded in the garden. Pun intended.