Mid-Senior Engineers: Reinvent your career with Robotics, earning $100k-$200k+ in the next 90 days. Learn More

How to parse JSON Data with Arduino (ArduinoJson Tutorial)

Liz Miller Learn Robotics

About the Author, Liz Miller, Founder/CEO @ Learn Robotics

Liz graduated with a degree in Robotics Engineering from Worcester Polytechnic Institute and researched drones at UPenn's GRASP Lab. Liz is a former Raytheon Engineer, where she managed major $MM automation projects worldwide. Today, she's the driving force behind Learn Robotics, offering the Robotics Career Blueprint for Engineering Professionals and beginner courses through the Online Robotics Class. Liz is a third-generation entrepreneur who is all about the application of innovation in robotics, automation, and AI. Follow Liz on LinkedIn and Facebook.

Disclosure: Some of the links below are affiliate links. This means that, at zero cost to you, Learn Robotics will earn an affiliate commission if you click through the link and finalize a purchase. Learn Robotics is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a way for websites to earn advertising revenues by advertising and linking to Amazon.com.

Join our Private Discord Server, MakeRobots! Sign Up

Update August 2022: Catch this Episode of Learn Robotics LIVE and the other 7 Special Topics Projects in our Special Topics Course and get up to speed on building this project and others from our Live Training Event.

In this article, learn how to parse JSON data with Arduino for IoT, IIoT, and Home Automation projects. We’ll start by connecting our wifi-enabled chip (ESP8266) to the Internet and then pointing it to the URL where the JSON file is stored. Then, we’ll use the ArduinoJson library to parse the JSON file into readable variables. Lastly, we’ll make checks against the variables to control outputs on our ESP8266.

JSON data is a lot easier to work with using Python, but if you’re creating an IoT device using Arduino, then this methodology comes in handy. Expect to spend an hour or so configuring this project.

This project is more advanced, so I recommend brushing up on the following topics if you’re new to Arduino and IoT.

The goal of this project is to create a secondary IoT device that receives data. If you’ve followed along with previous dweet.io tutorials, you’ll notice we spend a lot of time publishing data to a webpage. This tutorial expands on that project by creating a second device that fetches and interprets the published data.

If you’re looking to create your custom automation systems, then this can be very powerful because you’re essentially connecting two separate devices using the Internet.

Here’s a diagram of what the final solution looks like.

ArduinoJson and Dweet.io for Internet of Things

Now, let’s walk through the steps to get this working.

Materials for this Project

If you plan on using one device to publish data and another device to receive data, then you’ll need two separate ESP8266 controllers.

We’ll be demonstrating the project by sending data to the dweet.io website using a Wemos D1 Mini and then receiving data from that URL using a second device (NodeMCU). Lastly, we’ll parse the JSON file and use the data to control our LEDs.

Step 1: Wire Both Devices

Wire a potentiometer to the Wemos D1 Mini. Then, wire three LEDs to the NodeMCU. Here are some fritzing diagrams for your reference.

wiring diagrams wemos d1 mini and nodemcu for IoT project

Once your devices are wired, write and upload code to the Wemos D1 Mini to publish potentiometer data to dweet.io. We have an example of this in our Special Topics Robotics Course and this article. Then move on to the next step.

Step 2: Configure ArduinoJson Library

Install ArduinoJson library in Arduino IDE

To parse JSON files using Arduino, you’ll need to install the ArduinoJson library. Open up the Arduino IDE and go to Sketch > Include Library > Manage Libraries. Then search for ArduinoJson by Benoit Blanchon. Click the Install button. Then, include the header file at the top of your Arduino sketch.
#include <ArduinoJson.h>

Then, set up the sketch as you would normally. Define any global variables. Initialize the Serial Monitor at 115200 baud, and define any pin modes in the method. You’ll also need to connect the controller to the Internet.

Once you have the code written, it’s time to add some logic to fetch and parse JSON Data.

Are you more of a visual learner? Sign up for the Special Topics Robotics Course to gain access to the video lesson of this project!

Step 3: View raw JSON data at the URL (dweet.io)

The first step is to view the raw JSON data hosted on dweet.io. Regardless of whether you publish this data from a device or use an existing device, it’s important to understand how the data is formatted so that we can receive it in a way that makes sense.

Open up your browser and go to dweet.io/see. Then click on the device name to see the current readings.

Find the raw JSON URL by going to dweet.io/get/latest/dweet/for/MYTHING. Replace “MYTHING” with the name of your thing. For this example, the thing name is “wemos,” so we’ll go to the URL https://dweet.io/get/latest/dweet/for/wemos

Step 4: Fetch Data from dweet.io using Arduino

Now, we’re ready to use ArduinoJson to fetch this data. Copy and paste the JSON information from dweet.io into the ArduinoJson V6 Assistant tool. This will give you the necessary notation to format and parse the JSON file correctly using the ArduinoJson library.

manipulate JSON data with Arduino IoT devices

For this example, we care most about the potentiometer position, so we can update the variable for the potentiometer value to something more meaningful than int with_0_content_position. I’ve renamed this to potPosition for clarity. You can choose to use the generated code from the assistant tool, or modify it to best suit your application. I chose to print out the potentiometer value to the serial monitor once I receive it from the dweet.io website.

   //add print-outs to Serial Monitor
      Serial.print("Potentiometer Position: ");
      Serial.println(potPosition);

Step 5: Make Decisions based on Key-Value Pairs

Once you have the data, you can do whatever you’d like with it. Essentially, you have a local variable that can be used to toggle LEDs or local outputs based on conditions.

Let’s use the value of the potentiometer position to turn on the corresponding LED based on the following conditions.

ESP8266 with dweet.io

If the position is less than 500, turn on the red LED. If the position is less than 750 and greater than 500, turn on the yellow LED. If the position is greater than 850, turn on the green LED. I chose to use global variables to hold these thresholds. You can opt to just hardcode the values if you’d like.

Here’s some example code that you can use to set up your device.

// global variables
int redLimit = 500;
int yellowLimit = 850;

// set LED state
      if(potPosition <= redLimit){
        digitalWrite(red, HIGH);
        digitalWrite(yellow, LOW);
        digitalWrite(green, LOW);
        Serial.println("Red LED ON");
      }
      else if(potPosition <= yellowLimit){ 
        digitalWrite(yellow, HIGH); 
        digitalWrite(green, LOW); 
        digitalWrite(red, LOW); 
        Serial.println("Yellow LED ON"); } 
      else if(potPosition > yellowLimit){
        digitalWrite(green, HIGH);
        digitalWrite(yellow, LOW);
        digitalWrite(red, LOW);
        Serial.println("Green LED ON");
      }

And that’s it! You can choose to update the local variable in real-time, store data into an array, create a CSV, or whatever else makes sense for your application. Once you have the data, the possibilities are endless!

Next Steps for Further Learning

In this tutorial, we learned how to parse a JSON file using Arduino. The data was stored on the dweet.io website, which we connected to and performed a GET HTTP request. Once we removed the headers and allocated the JSON file, we could use the ArduinoJson library to parse the key-value pairs and set a local variable. In turn, we were able to use this variable to check against a set of conditions and turn on a corresponding LED.

You can use this methodology to configure a network of IoT devices. These devices can automatically connect to a webpage, push and pull data, and make decisions based on the current values. As a result, you can unlock powerful, real-time metrics for applications in Industrial and Home Automation.

Join the Online Course: Be sure to sign up for the Special Topics Robotics Course to access starter code and earn a certificate for completing each of these projects.

If you enjoyed this tutorial, be sure to share it with a friend!

Experienced Engineer (Mechanical, Electrical, Computer, Software): If I offered to help you upgrade your engineering career to robotics, earning $100k-$200k+ in the next 90 days, would you take me up on that offer? Click here for details.
Liz Miller Learn Robotics

🚀 Pre-Launch: Become a "MakeR" with MakeRobots!

Hey Reader, 👋

Liz Miller, Founder/CEO, here with some Exciting News!

Learn Robotics just acquired MakeRobots™, an Online Robotics Community, and are prepping its Official Learn Robotics Debut in Late 2023.

MakeRobots™ is your one-stop-shop for learning, gaining coding, electronics, and robotics skills, connecting, and building robots for one low monthly membership!

Join MakeRobots™ at our Special Pre-launch Rate!
🤖 Access our Private Community & Robotics Courses
💬 Network, Collaborate, Connect with Other Makers
🔓 Only $5.99/month – locked in for life
⏱️ Pre-launch deal is Limited to the first 1,000 subscribers

This is a perfect opportunity for you to get into the fastest growing robotics community on the internet, at our ground-level, pre-launch membership rates.

👇 Click the button below to Claim your Pre-launch Membership and become a MakeR in the MakeRobots Community, today!

Learn Robotics Botly Favicon

MORE LEARN ROBOTICS ARTICLES