-
Notifications
You must be signed in to change notification settings - Fork 23
Object to JSON string and back ...
When we want to pass around (Javascript) objects, those objects needs to be converted somehow to texts and back to objects.
For example to store a (Javascript) object in a file, that object needs to be converted to a text. To load that same object again from the file, the text needs to be converted back to an object.
In Javascript, objects are converted to and from JSON strings. JSON stands for Javascript Object Notation, and is a lightweight format to represent an object. This means that is a very compact format, compared to e.g. XML.
The following animation shows how these steps can be accomplished:
- An input message arrives with a 'payload' and a 'topic' field.
- That input message object will be converted to a JSON string.
- And then that JSON string is converted back to an object.
Remark: in the debug panel it becomes obvious that the JSON is a flat text, while the (Javascript) message object can be nicely expanded to show all its properties and corresponding values.
Messages are passed in Node-RED as (Javascript) objects, not as JSON text!