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.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 compliment my NodeRED for beginners series. Go and check it out if you are into home automation.

Support NotEnoughTech
A lot of time and effort goes into keeping NotEnoughTech alive! If my work helped you out, consider buying me a coffee or check out exclusive rewards available to Patreon supporters.
SHARE