How to work with JSON in an Arduino project

Oscar de la Hera Gomez
A flower that represents Arduino with the text "JSON" beneath it.

A guide for setting up an Arduino project to work with JSON, including a sample use case.

We recommend that you clone our Open Source Arduino Starter Project, checking out the main branch and carrying out the steps below. The changes can be found on the tutorial/json branch.

git clone git@github.com:delasign/arduino-starter-project.git

Step One: Install the JSON Library

A screenshot of the Arduino IDE highlighting how you can install the Arduino JSON library.

Using the Library Manager, the relevant JSON library.

We recommend that you use ArduinoJson library by Benoit Blanchon.

Step Two: Create the Models

A screenshot of the Arduino IDE showing the structs that we created for the SampleJSONObject model.

Create a Header and CPP file for the models and integrate the structs within the header file.

Please note that the CPP file needs to solely include the models.h file.

Step Three: Create the JSON Manager

A screenshot of the Arduino IDE showing the header file for the JSON Manager Singleton Class.

Create a Header and CPP file for the JSON Manager and code it so that it behaves as a singleton.

Add the conversion functionality that's required to convert JSON objects to strings and vice versa.

You should also include utilities that will transform the models in Step Two to JSON and back.

Code for the Header File

Code for the CPP File

Step Four: Implement

A screenshot of the Arduino IDE showing the functionality that we created to test that objects can be converted to strings and back.

In the Arduino ino file:

  • Include the JSON Manager and Models header files.
  • Add functionality to test the conversion to JSON and back.

Step Five: Test

A screenshot of the Arduino IDE showing that the Serial Monitor's logs demonstrate that the process is working.

Upload the code onto your board and confirm that the code works as expected.

Looking to learn more about things you can do with Arduino ?

Search our blog to find educational content on Arduino.

Any Questions?

We are actively looking for feedback on how to improve this resource. Please send us a note to inquiries@delasign.com with any thoughts or feedback you may have.
delasign logo

Book a Free Consultation.

An icon of an email.

Click here to email us.

Fill in the details below to book a free consultation or to let us know about something else. Whatever it is, we are here to help.

How can we help you ?

Contact Details