How to Work With JSON in Node-RED

Introduction: How to Work With JSON in Node-RED

This instructable will teach you how to work with JSON in node-RED. I will show you how to control networked power sockets with transfering json files via http get and post. And you can use this knowledge later to control any device supporting JSON protocol.

For teaching purposes i will be using NETIO 4All networked power socket, but dont worry, you dont need to buy anything. NETIO has amazing 4All online demo you can use.


NETIO 4All networked powersocket or 4All online demo:


Step 1: REST JSON Http(s) Protocol

This part is a little bit technical but please bear with me. If I didn´t explain this part then you would have problems understanding the values in json files we will be sending in node-RED.

Actions applicable to each output (electrical socket):

In all M2M protocols, NETIO power sockets use the same actions that can be applied to individual outputs. For example, a Toggle or Short Off action can be written to any output.

However, the Action variable can only be used for writing values, it cannot be used to read the current outlet state.

These are actions you can apply to each output:

0 = Output switched off (Off)

1 = Output switched on (On)

2 = Output switched off for a short time (short Off)

3 = Output switched on for a short time (short On)

4 = Output switched from one state to the other (toggle)

5 = Output state unchanged (no change)

6 = Ignored

Example – JSON file to toggle the output no. 1:


"Outputs": [{

"ID": 1,

"Action": 4



ID - this number indicates which output we will be using

Action - this part, is the action that the output will make (e.g. 1 (Turn output on))

Step 2: Flow

And now the main part. This is how node-RED environment looks like.

We have imported URL API flow (The project you see. Later i will show you how to import flows and this project) This project consists of two parts:

  1. NETIO AN30 (JSON REST API) flow
  2. Dashboard (Graphical interface by which you can operate your program)

Step 3: Dashboard

This is how dashboard in node-RED for this instructable looks like. You can customize it if you want to suit your taste.

The dashboard for this project is divided into 4 parts:

  1. Device Status - shows device information such as model, mac address or firmware version.
  2. (POST) Control Output 1 - Contains 5 buttons which controls output 1. Each button executes different action
  3. (GET) O1 - O4 Output States - This part shows current state of each output from your device.
  4. Device Management - In this part you can find all sorts of graphs and gauges which are showing current metered values from NETIO 4Aall device

Step 4: Import Flow (project, Script, Etc)

In the menu (right upper corner) select Import and then Clipboard.

Then, copy the text below to the indicated field and click Import.

Installing missing nodes

The nodes are loaded into the selected flow. It is possible that an error message is displayed with a list of nodes that are being imported but are not installed in Node-RED yet. In this case, the missing nodes need to be installed.

If there are nodes missing, select Manage palette in the menu, click Install and find and install the nodes you are missing.

Import text:

[{"id":"56b9510c.98c6f","type":"tab","label":"NETIO AN30 (REST JSON)","disabled":false,"info":""},{"id":"6a66b637.da1558","type":"http request","z":"56b9510c.98c6f","name":"HTTP Request (POST)","method":"POST","ret":"txt","url":"","tls":"","x":430,"y":100,"wires":[["9cc48ba4.233128"]]},{"id":"9cc48ba4.233128","type":"debug","z":"56b9510c.98c6f","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":630,"y":100,"wires":[]},{"id":"aa32bf8.f83704","type":"http request","z":"56b9510c.98c6f","name":"HTTP Request (GET)","method":"GET","ret":"txt","url":"","tls":"","x":520,"y":240,"wires":[["23bf436c.8d90bc"]]},{"id":"23bf436c.8d90bc","type":"json","z":"56b9510c.98c6f","name":"JSON Parse","property":"payload","action":"","pretty":false,"x":710,"y":240,"wires":[["3481543c.05cefc"]]},{"id":"3481543c.05cefc","type":"function","z":"56b9510c.98c6f","name":"Function","func":"msg.Model = msg.payload.Agent.Model;\nmsg.Version = msg.payload.Agent.Version;\nmsg.Time = msg.payload.Agent.Time;\nmsg.JSONVersion = msg.payload.Agent.JSONVer;\nmsg.SerialNumber = msg.payload.Agent.SerialNumber;\n\ntry{msg.Voltage = msg.payload.GlobalMeasure.Voltage}\n catch (err1) {msg.Voltage = 0;}\ntry{msg.Frequency = msg.payload.GlobalMeasure.Frequency}\n catch (err2) {msg.Frequency = 0;}\ntry{msg.TotalCurrent = msg.payload.GlobalMeasure.TotalCurrent / 1000}\n catch (err3) {msg.TotalCurrent = 0;}\ntry{msg.OverallPowerFactor = msg.payload.GlobalMeasure.OverallPowerFactor}\n catch (err4) {msg.OverallPowerFactor = 0;}\ntry{msg.TotalLoad = msg.payload.GlobalMeasure.TotalLoad}\n catch (err5) {msg.TotalLoad = 0;}\n\nmsg.O1_State = msg.payload.Outputs[0].State;\nmsg.O2_State = msg.payload.Outputs[1].State;\nmsg.O3_State = msg.payload.Outputs[2].State;\nmsg.O4_State = msg.payload.Outputs[3].State;\n\nif(msg.O1_State) msg.O1_Color = \"#09ef28\";\n else msg.O1_Color = \"Orange\";\nif(msg.O2_State) msg.O2_Color = \"#09ef28\";\n else msg.O2_Color = \"Orange\";\nif(msg.O3_State) msg.O3_Color = \"#09ef28\";\n else msg.O3_Color = \"Orange\";\nif(msg.O4_State) msg.O4_Color = \"#09ef28\";\n else msg.O4_Color = \"Orange\";\n\nreturn msg;","outputs":1,"noerr":0,"x":860,"y":240,"wires":[["d764a23a.3c531"]]},{"id":"76fd06c4.0f36f8","type":"ui_text","z":"56b9510c.98c6f","group":"470e04a4.87c72c","order":1,"width":0,"height":0,"name":"NETIO model","label":"NETIO Model: ","format":"{{msg.Model}}","layout":"row-center","x":660,"y":360,"wires":[]},{"id":"8345c1eb.4a0e6","type":"ui_text","z":"56b9510c.98c6f","group":"470e04a4.87c72c","order":2,"width":0,"height":0,"name":"","label":"Model Firmware","format":"{{msg.Version}}","layout":"row-center","x":660,"y":400,"wires":[]},{"id":"7ed7e52e.27477c","type":"ui_text","z":"56b9510c.98c6f","group":"470e04a4.87c72c","order":3,"width":0,"height":0,"name":"Serial Number","label":"Serial Number","format":"{{msg.SerialNumber}}","layout":"col-center","x":660,"y":440,"wires":[]},{"id":"4852076c.3868e8","type":"ui_text","z":"56b9510c.98c6f","group":"470e04a4.87c72c","order":4,"width":0,"height":0,"name":"JSON Version","label":"JSON Version:","format":"{{msg.JSONVersion}}","layout":"row-center","x":660,"y":480,"wires":[]},{"id":"7472ee86.79bc3","type":"ui_gauge","z":"56b9510c.98c6f","name":"Current (Device)","group":"a7539552.a04768","order":4,"width":0,"height":0,"gtype":"gage","title":"Current ({{msg.Model}})","label":"A","format":"{{msg.TotalCurrent}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":300,"y":400,"wires":[]},{"id":"3068d819.5ff718","type":"ui_gauge","z":"56b9510c.98c6f","name":"Overall Power Factor","group":"a7539552.a04768","order":3,"width":0,"height":0,"gtype":"gage","title":"Overall Power Factor","label":"","format":"{{msg.OverallPowerFactor}}","min":0,"max":"1","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":320,"y":440,"wires":[]},{"id":"323990ed.14cf5","type":"ui_gauge","z":"56b9510c.98c6f","name":"Voltage","group":"a7539552.a04768","order":6,"width":0,"height":0,"gtype":"gage","title":"Voltage","label":"V","format":"{{msg.Voltage}}","min":0,"max":"500","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":280,"y":480,"wires":[]},{"id":"3e88e47a.74d26c","type":"ui_gauge","z":"56b9510c.98c6f","name":"Frequency","group":"a7539552.a04768","order":5,"width":0,"height":0,"gtype":"gage","title":"Frequency","label":"Hz","format":"{{msg.Frequency}}","min":0,"max":"100","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":290,"y":520,"wires":[]},{"id":"9b0f772f.e0fc58","type":"ui_chart","z":"56b9510c.98c6f","name":"Current Chart (Device)","group":"a7539552.a04768","order":2,"width":0,"height":0,"label":"Current ","chartType":"line","legend":"true","xformat":"HH:mm","interpolate":"linear","nodata":"","dot":false,"ymin":"0","ymax":"32","removeOlder":1,"removeOlderPoints":"","removeOlderUnit":"60","cutout":0,"useOneColor":false,"colors":["#ff0000","#aec7e8","#ff7f0e","#2ca02c","#98df8a","#0000a0","#ff9896","#9467bd","#c5b0d5"],"useOldStyle":false,"x":320,"y":360,"wires":[[],[]]},{"id":"51626bea.79f244","type":"link in","z":"56b9510c.98c6f","name":"","links":["d764a23a.3c531"],"x":75,"y":400,"wires":[["7472ee86.79bc3","3068d819.5ff718","323990ed.14cf5","3e88e47a.74d26c","3567a4c0.0ee3cc","c3c29296.4505d"]]},{"id":"d764a23a.3c531","type":"link out","z":"56b9510c.98c6f","name":"Link","links":["51626bea.79f244","57f3c961.d9b3e8","bda11677.919808"],"x":955,"y":240,"wires":[]},{"id":"bda11677.919808","type":"link in","z":"56b9510c.98c6f","name":"","links":["d764a23a.3c531"],"x":495,"y":440,"wires":[["76fd06c4.0f36f8","8345c1eb.4a0e6","7ed7e52e.27477c","4852076c.3868e8"]]},{"id":"3567a4c0.0ee3cc","type":"function","z":"56b9510c.98c6f","name":"Current Chart","func":"msg.payload = msg.TotalCurrent;\nreturn msg;","outputs":1,"noerr":0,"x":160,"y":300,"wires":[["9b0f772f.e0fc58"]]},{"id":"c3c29296.4505d","type":"ui_gauge","z":"56b9510c.98c6f","name":"","group":"a7539552.a04768","order":1,"width":0,"height":0,"gtype":"gage","title":"Total Load","label":"W","format":"{{msg.TotalLoad}}","min":0,"max":"10000","colors":["#00b500","#e6e600","#ca3838"],"seg1":"","seg2":"","x":290,"y":560,"wires":[]},{"id":"ded04c51.0b31e","type":"inject","z":"56b9510c.98c6f","name":"1 Second Repeat","topic":"","payload":"","payloadType":"date","repeat":"1","crontab":"","once":false,"onceDelay":"1","x":310,"y":240,"wires":[["aa32bf8.f83704"]]},{"id":"763762d6.d52dec","type":"ui_button","z":"56b9510c.98c6f","name":"Output 1 = ON","group":"ab94b758.4be768","order":1,"width":0,"height":0,"passthru":false,"label":"Output 1 = ON","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"Outputs\":[{\"ID\":1,\"Action\":1}]}","payloadType":"json","topic":"","x":100,"y":40,"wires":[["6a66b637.da1558"]]},{"id":"3930df24.761ab","type":"ui_button","z":"56b9510c.98c6f","name":"Output 1 = OFF","group":"ab94b758.4be768","order":2,"width":0,"height":0,"passthru":false,"label":"Output 1 = OFF","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"Outputs\":[{\"ID\":1,\"Action\":0}]}","payloadType":"json","topic":"","x":100,"y":80,"wires":[["6a66b637.da1558"]]},{"id":"3f0ec8e0.d39668","type":"ui_button","z":"56b9510c.98c6f","name":"Output 1 = Short ON","group":"ab94b758.4be768","order":3,"width":0,"height":0,"passthru":false,"label":"Output 1 = Short ON","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"Outputs\":[{\"ID\":1,\"Action\":3}]}","payloadType":"json","topic":"","x":120,"y":120,"wires":[["6a66b637.da1558"]]},{"id":"26a70e42.0b8e12","type":"ui_button","z":"56b9510c.98c6f","name":"Output 1 = Short OFF","group":"ab94b758.4be768","order":4,"width":0,"height":0,"passthru":false,"label":"Output 1 = Short OFF (Restart)","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"Outputs\":[{\"ID\":1,\"Action\":4}]}","payloadType":"json","topic":"","x":120,"y":160,"wires":[["6a66b637.da1558"]]},{"id":"4b19bca6.15d3f4","type":"ui_button","z":"56b9510c.98c6f","name":"Output 1 = Toggle","group":"ab94b758.4be768","order":5,"width":0,"height":0,"passthru":false,"label":"Output 1 = Toggle","tooltip":"","color":"","bgcolor":"","icon":"","payload":"{\"Outputs\":[{\"ID\":1,\"Action\":2}]}","payloadType":"json","topic":"","x":110,"y":200,"wires":[["6a66b637.da1558"]]},{"id":"57f3c961.d9b3e8","type":"link in","z":"56b9510c.98c6f","name":"","links":["d764a23a.3c531"],"x":795,"y":420,"wires":[["33465792.5b26e8","26696a86.9322d6","7c1508d2.f869a8","c6c0aad.141fa58"]]},{"id":"33465792.5b26e8","type":"ui_text","z":"56b9510c.98c6f","group":"a5e4e892.40d1b8","order":1,"width":"0","height":"0","name":"O1 State","label":" O1 =","format":" {{msg.O1_State}} ","layout":"row-center","x":920,"y":360,"wires":[]},{"id":"26696a86.9322d6","type":"ui_text","z":"56b9510c.98c6f","group":"a5e4e892.40d1b8","order":2,"width":"0","height":"0","name":"O2 State","label":" O2 =","format":" {{msg.O2_State}} ","layout":"row-center","x":920,"y":400,"wires":[]},{"id":"7c1508d2.f869a8","type":"ui_text","z":"56b9510c.98c6f","group":"a5e4e892.40d1b8","order":3,"width":"0","height":"0","name":"O3 State","label":" O3 =","format":" {{msg.O3_State}} ","layout":"row-center","x":920,"y":440,"wires":[]},{"id":"c6c0aad.141fa58","type":"ui_text","z":"56b9510c.98c6f","group":"a5e4e892.40d1b8","order":4,"width":"0","height":"0","name":"O4 State","label":" O4 =","format":" {{msg.O4_State}} ","layout":"row-center","x":920,"y":480,"wires":[]},{"id":"470e04a4.87c72c","type":"ui_group","z":"","name":"Device Status","tab":"23994b37.39eb44","order":1,"disp":true,"width":"6","collapse":false},{"id":"a7539552.a04768","type":"ui_group","z":"","name":"Device Management","tab":"23994b37.39eb44","order":4,"disp":true,"width":"6","collapse":false},{"id":"ab94b758.4be768","type":"ui_group","z":"","name":"Control Output 1","tab":"23994b37.39eb44","order":2,"disp":true,"width":"6","collapse":false},{"id":"a5e4e892.40d1b8","type":"ui_group","z":"","name":"O1 - O4 Output States","tab":"23994b37.39eb44","order":3,"disp":true,"width":"6","collapse":false},{"id":"23994b37.39eb44","type":"ui_tab","z":"","name":"NETIO AN30 (REST JSON)","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

Step 5: How It All Works Together

The flow is basically divided into two parts: POST and GET.

POST: Writing to O1

  • Five buttons created in the Dashboard in the Flow are displayed in the Dashboard.
  • After clicking the Output 1 = ON button in the Dashboard, the payload is set to the netio.json file that specifies the output and the action (defined for each of the buttons).
  • The HTTP Request (POST) block sends the netio.json file as a request to an IP address.
  • The server response (status) is returned as the output.
  • The Msg.payload block displays the result from HTTP Request (POST).

GET: Reading from O1 – O4

  • The 1 Second Repeat block activates, with a period of one second, the HTTP Request (GET) block, which in turn sends netio.json as a GET request and returns a complete JSON file with the socket status as received from the server.
  • The JSON Parse block transforms the JSON file from the HTTP Request (GET) block to a JSON Object so that it is possible to manipulate the properties in the JSON file.
  • The Function block takes individual parts of the JSON object and transforms them into properties of a msg object for later use.
  • The Current Chart block sets msg.payload to the msg.TotalCurrent property of the msg object, because the subsequent Current Chart (Device) block can only display the msg.payload value.
  • Various output nodes then follow in order to display selected properties of the msg object, as taken from the JSON object, in the Dashboard.

The msg object and msg.payload

For a simple and concise explanation, see here:

Step 6: HTTP POST and GET

HTTP Request(POST)

This node sends a netio.json command file as a HTTP Request (POST) in order to control the NETIO 4All device.

HTTP Request(GET)

This node sends a HTTP Request (GET) and returns the status response.

Thepre-filled address points to the NETIO 4All online demo, where you can test the connection without having a NETIO device at your desk.

It is possible to set your own IP address in these nodes; however, the IP needs to be changed in both the HTTP Request nodes, POST as well as GET.

Step 7: Button Nodes

Clicking the button node generates a message containing a netio.json file(Right picture) which is then sent via http post node to netio smart power socket.

Step 8: Function Node

A function node is a special node that enables writing a custom JavaScript function.

In this instructable, the function picks values from the parsed JSON file (now a JSON object) and assigns them to the properties of the msg object.

The code is divided into four sections:

  1. Assigning values from the JSON object to the individual properties of the msg object
  2. Error handling in case the networked power socket does not support global measurements
    • If the networked power socket does not support the measurement of global values, Node-RED would display errors because this function would not find the respective property, e.g. msg.payload.GlobalMeasure.Voltage, since it would not be present in the JSON object. In this case, the property of the msg object, e.g. msg.Voltage, is set to 0 and the error is caught a handled.

  3. Assigning output state values

  4. Setting the colors of the displayed output state values according to the output states

Step 9: JSON Node and Inject Node

JSON node

JSON node parses the JSON file and transforms it into a JSON object.

As a response from the server to the GET request, the HTTP Request node returns a JSON file containing the current status of the NETIO 4x device, but is is simply a text file, so in order to work with the data, the JSON file needs to be parsed into a JSON object.

Inject node

Every second, this node activates the HTTP Request node that sends a GET request.

As a result, the values in the Dashboard are updated with a period of one second

Step 10: Text Node and Debug Node

Text node

Displays a text field in the Dashboard. In this instructable, the text nodes display the current, voltage, model, firmware version or JSON version.

The Label is displayed in the Dashboard, and the Name is the node name displayed in the flow in the Node-RED.

Debug node

Displays the msg.payload.

Step 11: Chart Node

This node plots the current chart in the Dashboard according to the payload value.

This node can onlyplot charts according to the payload value.

For this reason, a function node is used to set msg.payload to the value that needs to be displayed.

msg.payload = msg.TotalCurrent;

Step 12: Gauge Node and Link Nodes

Gauge node

This node adds a gauge widget to the Dashboard.

In this instructable, each gauge visualizes one property of the msg object: voltage [V], curent [A], frequency [Hz] and the overall True Power Factor (TPF).

Link nodes

Link in and link out nodes work like a tunnel. The msg.payload arrives into the link in node and goes out of the link out node.

I used it to make the flow a bit clearer and easier to read.

Step 13: Thank You for Reading My Instructable

I hope you enjoyed my instructable and hopeffuly learned something new.

This instructable is only shortened version of different guide I made.

The original guide is longer and much more detail oriented and generally better structured. If you dont understood something or think i missed or not explained enough something, then you can definitely find it there.

I promise you wont be dissapointed


Also there are similiar guides about various uses of
node-RED, so if you are interested feel free to explore:

Working with REST URL API in node-RED

Working with REST XML in node-RED

Working with TCP/Modbus in node-RED

Coming very soon :)

Be the First to Share


    • Game Design: Student Design Challenge

      Game Design: Student Design Challenge
    • Make It Bridge

      Make It Bridge
    • Big and Small Contest

      Big and Small Contest