Working with JSON Data And JavaScript Objects in Node-Red

JSON=NodeJSON 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

Creating a JSON string from a JavaScript object

var s=JSON.stringify(JavascriptObject);

Decoding

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.

JSON=Node-Properties

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:

working-with-JSON-data

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.

debug-node-display-JSON

Extracting Values from a JSON String

A Common requirement is to extract particular values from the incoming JSON data.

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 screenshot 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

JSON-Javascript-example

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.

Javascript-Object-examplre

To access the data we encounter the same problems without quotes.

Access-data_Javascript-object

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.

Variables-javascript-objects

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:

manual-Json-data

Entering JSON Data into the 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.

json-edito-node-red

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:

Related Tutorials:

Please rate? And use Comments to let me know more
[Total: 4    Average: 5/5]

Leave a Reply

Your email address will not be published. Required fields are marked *