HomeRaspberry PiEverything you need to start using JSON

Everything you need to start using JSON

It's JSON not Jason, just saying!

Before you read any further, this is a guide that aims to ease you into the JSON data structure. It’s written for the purpose of interaction with NodeRED and Tasker and may not be sufficient (or accurate enough) to give you all the programming basis for JavaScript. With that in mind, here is everything you need to start using JSON in your NodeRED, Tasker or HTTP requests.

Understanding and using JSON

Let’s say we want to send a weather data from one device to another, there are multiple ways of doing this, however, the more detailed the data gets, the fewer options we have. I will assume, I have a 3-day forecast with the following information:

Day1
Temp: 21°C
Wind: 15km/h
Conditions: Sunny
Temp across the day:
    Morning: 15°C
    Afternoon: 20°C
    Evening: 10°C
Humidity every hour: 22,23,25,26,27,26,25,20

I’m not going to list the data for the whole 3 days as you will get the point later. JSON is one of the best ways of sending the structured data.

JSON object

In NodeRED data sent between nodes is enclosed in an object called “message” or msg.  for short.  The JSON object looks like this:

msg = { };

The most commonly used JSON keys in NodeRED are payload and topic. JSON objects can store data using pairs of keys and values. The key describes the type of the data (temperature) while value represents, well the value that key stores (23°C). This is why we access it via msg.payload & msg.topic notation:

msg = {
    "payload":"some payload",
    "topic":"some topic"
};

The object can store an unlimited number of keys & values (they are all separated by the comma (,)) as long as the key’s names are unique. Keys should not contain spaces. Our weather data for the day one would look like this:

Day = {"day":1,
"Temp":"21°C",
"Wind":"15km/h",
"Conditions":"Sunny"
};

As you can see the keys and values are separated by (:) colon. You will also notice that most of the values are enclosed in “…” while integer (number, isn’t). The JSON  can store multiple data formats:

  • null (empty) 
  • whitespace ( )
  • value (string, integer, bool, null)
  • object (nested JSON which obeys JSON rules)
  • boolean (true/false)
  • string (value with enclosed in “..”)
  • number (integer or float – no need for “…”) 
  • array (ordered sequence of values)

Now that we know how the key & value pair works let’s add objects and arrays to the mix. I missed out the “Temp_across:” and “Humidity_every_hour:” entries.

Upon closer inspection you will see that the Temp across the day: is an object that consists 3 key & values pairs so let’s add this to our JSON:

Day = {"day":1,
"Temp":"21°C",
"Wind":"15km/h",
"Conditions":"Sunny",
"Temp_across":{
    "Morning":"15°C",
    "Afternoon":"20°C",
    "Evening":"10°C"}
};

Looking at the humidity data, we have a row of numbers that are posted every hour. The data is sorted in some order and to preserve this, I’m going to use an array. Arrays are enclosed in [..]  brackets and counting  (usually) starts from 0.

Day = {"day":1,
"Temp":"21°C",
"Wind":"15km/h",
"Conditions":"Sunny",
"Temp_across":{
    "Morning":"15°C",
    "Afternoon":"20°C",
    "Evening":"10°C"},
"Humidity_every_hour":[22,23,25,26,27,26,25,20]
};
or 
"Humidity_every_hour":["22","23","25","26","27","26","25","20"]
if I want to store the array elements as strings instead of integers

Suddenly, it’s easier to understand the JSON object like this (spoiler, this is an unprettified version of the object above):

Day={"day":1,"Temp":"21°C","Wind":"15km/h","Conditions":"Sunny","Temp_across":{"Morning":"15°C","Afternoon":"20°C","Evening":"10°C"},"Humidity_every_hour":[22,23,25,26,27,26,25,20]};

Accessing the data using JSON

The biggest advantage of JSON is how easy is to access the specific data from that object.  I’m still using the daily weather example. To access the temperature for that day – I need to reach the key “Temp” inside by Day object:

Day.Temp contains the 21°C

If I want to reach deeper and access the temperature in the morning, I would have to use:

Day.Temp_across.Morning contains the 15°C

Lastly, if I want to check what was the humidity like at the second reading (remember arrays start counting from 0):

Day.Humidity_every_hour[1] contains 23

Lastly, I mentioned that the weather forecast would be part of a 3-day forecast. You know the arrays already, so you know what’s gonna happen. Each day will be an entry inside the array:

weatherforecast ={
"Day"=[
{"day":1, "Temp":"21°C", "Wind":"15km/h", "Conditions":"Sunny", "Temp_across":{"Morning":"15°C",     "Afternoon":"20°C",     "Evening":"10°C"}, "Humidity_every_hour":[22,23,25,26,27,26,25,20]},
{"day":2, "Temp":"25°C", "Wind":"18km/h", "Conditions":"Sunny", "Temp_across":{"Morning":"15°C",     "Afternoon":"20°C",     "Evening":"10°C"}, "Humidity_every_hour":[22,23,25,26,27,26,25,20]},
{"day":3, "Temp":"26°C", "Wind":"10km/h", "Conditions":"Sunny", "Temp_across":{"Morning":"15°C",     "Afternoon":"20°C",     "Evening":"10°C"}, "Humidity_every_hour":[22,23,25,26,27,26,25,20]}
]
};

And accessing the morning temperature on the second day would be used with:

weatherforecast.Day[1].Temp_across.Morning

Which translates to access the position in the Day array inside the weatherforecast object and provide me with the Morning value stored inside the Temp_across.

I hope it all makes sense now!  Understanding how data is processed in the NodeRED and Tasker should be much easier. This write-up has been created to complement my NodeRED for beginners series. Go and check it out if you are into home automation.

PayPal

Nothing says "Thank you" better than keeping my coffee jar topped up!

Patreon

Support me on Patreon and get an early access to tutorial files and videos.

image/svg+xml

Bitcoin (BTC)

Use this QR to keep me caffeinated with BTC: 1FwFqqh71mUTENcRe9q4s9AWFgoc8BA9ZU

M5Paper

Programable, ESP32 based awesome dev platform with 4.7 e-ink display by M5Stack

More HATs

client-image
client-image

Argon One M.2

Enclose Raspberry Pi 4 inside this great case with custom I/O, cooling and GPIO and M.2 SSD support

More cases on

client-image
client-image

Best Raspberry Pi Projects

How to use Raspberry PI as WOL (wake on lan) server

0
While you could wake up your PC from a mobile directly, having a dedicated server capable of doing so is the best solution. The reason is simple. You can hook up as many devices as you wish with a single endpoint. This is why Raspberry Pi is perfect for this.

Slow Internet Warning

0
From time to time my Internet grinds to a stop. Since Raspberry Pi 4 comes with a 1Gbps Ethernet, I decided to take advantage of it and create a reporting system in NodeRED that will monitor and report when the ISP is not keeping the contractual agreements. Works with Alexa, Google Home, Android and Windows 10.

How fast Raspberry Pi NAS is?

0
Let's see how fast Raspberry Pi NAS really is?

Argon18: Argon ONE SSD modification

0
Argon One case just got better - now you can boot it from USB without ruining the design thanks to Argon 18: Argon One SSD modification

HOW TO...

It took me 2 months to boot CM4 from NVMe

0
Complete beginners guide to Compute Module 4 boot from NVMe.

Raspberry Pi Zero 2 W vs other Zero boards

0
It's time to test the Raspberry Pi Zero 2 W against other Raspberry Pi boards from Zero series: power, WiFi, temperature and core performance

C/C++ and MicroPython SDK for Raspberry Pi Pico on Windows

0
A guide to SDK toolchain for Raspberry Pi Pico and C/C++ , Micropython on Windows.

A comprehensive guide to Grafana & InfluxDB

0
How to use Grafana and InfluxDB on Raspberry Pi for IoT sensors in home automation

How to boot Raspberry Pi 4 from USB

0
How to set up and boot Raspberry Pi 4 from USB drive - headless guide.