JSON

What's the JSON node in Node-RED used for?

The JSON node in Node-RED is used for processing JavaScript Object Notation (JSON) data. It allows you to convert between a JSON-formatted string and a JavaScript object, making it highly versatile for tasks involving JSON manipulation. This conversion capability proves to be essential when working with APIs, storing data, or transmitting data between different services.

Use Case: Monitoring Equipment Efficiency

Suppose you have a JSON data stream that comes from different sensors installed on an assembly line in a manufacturing plant. The JSON objects include data such as equipment name, timestamp, and efficiency percentage. You'd like to extract this information and calculate a daily average efficiency for each equipment to help in predictive maintenance and to optimize the production process.

JSON Input:

{
"equipment": "Drill Press",
"timestamp": "2023-09-22T12:34:56Z",
"efficiency": 89.5
}

Flow

Node Documentation

Converts between a JSON string and its JavaScript object representation, in either direction.

Inputs

payloadobject | string
A JavaScript object or JSON string.
schemaobject
An optional JSON Schema object to validate the payload against. The property will be deleted before the msg is sent to the next node.

Outputs

payloadobject | string
  • If the input is a JSON string it tries to parse it to a JavaScript object.
  • If the input is a JavaScript object it creates a JSON string. The string can optionally be well-formatted.
schemaErrorarray
If JSON schema validation fails, the catch node will have a schemaError property containing an array of errors.

Details

By default, the node operates on msg.payload, but can be configured to convert any message property.

The node can also be configured to ensure a particular encoding instead of toggling between the two. This can be used, for example, with the HTTP In node to ensure the payload is a parsed object even if an incoming request did not set its content-type correctly for the HTTP In node to do the conversion.

If the node is configured to ensure the property is encoded as a String and it receives a String, no further checks will be made of the property. It will not check the String is valid JSON nor will it reformat it if the format option is selected.

For more details about JSON Schema you can consult the specification here.