JSON is popular format for encoding data sent over the Internet, and also stored in files.
In computing, JavaScript Object Notation (JSON) is an open-standard file format that uses human-readable text to transmit data objects consisting of attribute–value pairs and array data types (or any other serializable value). –wiki
Encoding and Decoding JSON Data
You can encode and decode JSON data using the JavaScript functions JSON.stringify() and JSON.parse() or the JSON Node.
Encoding JSON Data
Creating a JSON string from a JavaScript object
var s=JSON.stringify(JavascriptObject);
Decoding JSON Data
Creating a JavaScript object from a JSON string
var o=JSON.pase(JSONString);
The JSON node located under the functions category is capable of converting between a JSON string and JavaScript object and vice versa.
Receiving JSON Data
The first thing you need to do when you receive JSON data is to convert it into a JavaScript object.
Once you have a JavaScript object you can work on the data.
The two approaches using the JSON node and the JSON.parse() command are illustrated below:
Is it JSON Data or a JavaScript Object?
One of the most common problems that I’ve seen is trying to extract data from a JSON string thinking that it was a JavaScript object.
In Node-red if you pass the data into the debug node then it will show you the data and tell you the data type.
JSON data is a string and is surrounded in quotes as shown below.
Extracting Values from a JSON String
A Common requirement is to extract a particular value or values from the incoming JSON data.
To do that the first thing we must do is to convert the JSON string into a Javascript object.
Once we have a JavaScript object we can extract individual values using the key in dot format or quotes.
The following screen shot illustrates the process, using the command line ,along with some common problems that you might encounter like:
SyntaxError: Unexpected token o in JSON at position 1
Note: The node command line doesn’t require var declaration or ; to terminate the line. You will need them in the script in the function node.
JavaScript Object Notes:
Generally a JavaScript object key doesn’t need quotes. In the example above we used.
var o={temp:20,humidity:50};
and not
var o={"temp":20,"humidity":50};
However both are valid.
There are various rules on whether or not quotes are needed (see here). However it is usually best to use quotes to avoid confusion.
When accessing a value in a JavaScript Object you can use:
var value=o.temp;
or
var value= o["temp"];
The bracketed option will always work the dot notation will work depending on the key name.
The following screen shot illustrates assigning values to objects using the node command line.
To access the data we encounter the same problems without quotes.
Using Variables as Keys
Using a variables as an object key is a common requirement.
Again you find that you are required to use the bracketed option and not the dot notation.
var light="light1"; var o={}; o[light]="on";
Again We illustrate using the node command line.
Manually Entering JSON Data
When testing it is often necessary to create test data.
It is relatively easy to hand code Simple JSON data, however for more complex data I would recommend using the node command line to create the JSON Data from a JavaScript object.
In JSON all string values must be in quotes. See here
var s={temp:10}; //create Javascript object var s='{"temp":10}'; var s="{\"temp\":10}";
as shown below:
Entering JSON Data into the Node-Red Inject Node
Select the JSON option and enter the data using quotes around strings.
The JSON edit will show you if you have entered an invalid format.
Using the Mosquitto Publish Tool With JSON
With the mosquitto_pub tool you will need to escape the quotes and enclose it in quotes.E.G.
mosquitto_pub -h localhost -t test -m "{\"v1\/lights\":1}"
Notice also how I needed to delimit the forward slash in the key name.
Working With JSON Data in Node Red
Related videos:
I have created a number of videos on JSON that you might find useful:
- Publish JSON Data over MQTT with Node-Red
- Receive JSON Data Over MQTT with Node-Red
- How to Log MQTT Sensor Data in Node-Red
- Node-Red Guide to Using The CSV Node
Related Tutorials:
- Understaning and Using The Node-Red Message Object
- Understanding and Using Buffers In Node and Node-Red
- How to Send and Receive JSON Data Over MQTT with Python