Home Raspberry Pi Everything 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 in style with crypto-currency

SATA HAT

Add an SSD to Raspberry Pi 4 and turn it into a super-cooled NAS drive.

More HATs

Argon One

Enclose Raspberry Pi 4 inside this great case with custom I/O, cooling and GPIO

More cases on

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.

Free wireless doorbell (Amazon Dash button hack)

0
The most terrifying situation I can imagine* is missing a parcel

Raspberry Pi NAS: (NAS-pi) Modding like a boss #part2

0
Plex and RAID1 NAS drive build log

MagPi – auto-downloader

0
Grab each edition of Mag Pi as soon as possible!

HOW TO...

How to boot Raspberry Pi 4 from USB

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

Raspberry Pi Zero, Raspberry Pi 3A+ WiFi without keyboard or cables

0
Getting Raspberry Pi boards connected in seconds

Everything you need to start using JSON

0
It's JSON not Jason, just saying!

How to use Join App in NodeRED

0
The day finally comes where Join app is almost everywhere!

Driving LCD with Arduino and Raspberry Pi

0
Displaying things on an LCD screen is as easy as going through a cup of jellybeans while writing this tutorial!