JSON

Converts between JSON strings and JavaScript objects.

Where and why do we use the JSON node?

The JSON node processes JavaScript Object Notation (JSON) data. It converts between JSON-formatted strings and JavaScript objects, making it essential when working with APIs, storing data, or transmitting information between different services. This bidirectional conversion lets you parse incoming JSON data for processing and format JavaScript objects into JSON strings for output.

Modes of operation

The JSON node operates in two directions depending on what it detects in the input:

JSON String to Object

When the input is a JSON string, the node parses it into a JavaScript object. This mode is essential when receiving data from APIs, reading JSON files, or processing JSON payloads from HTTP requests. Once converted to an object, you can access and manipulate the data using standard JavaScript operations.

Object to JSON String

When the input is a JavaScript object, the node converts it into a JSON string. Use this mode when preparing data to send to APIs, writing to files, or transmitting structured data. You can optionally format the output with indentation for improved readability.

Automatic Detection

The node automatically detects whether the input is a JSON string or JavaScript object and performs the appropriate conversion. You can also configure it to always convert in a specific direction or validate JSON without conversion.

How the node handles messages

The JSON node processes the msg.payload property by default, but you can configure it to work with any message property. After conversion, it replaces the property with the converted value.

When parsing JSON strings, the node validates the syntax and reports errors if the JSON is malformed. When converting objects to strings, it handles nested structures, arrays, and standard JavaScript data types (strings, numbers, booleans, null).

The node can format JSON output with pretty printing, adding indentation and line breaks to make the structure more readable. This is useful for debugging or generating human-readable output files.

Examples

Monitoring equipment efficiency

Suppose you have a JSON data stream from sensors installed on an assembly line in a manufacturing plant. The JSON objects include equipment name, timestamp, and efficiency percentage. This flow extracts the information and calculates a daily average efficiency for each equipment to help with predictive maintenance and production optimization.

JSON Input:

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

The flow parses incoming JSON strings, groups messages together, calculates the average efficiency, and converts the result back to JSON format.

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.