Introduction
When using node-red to handle various business scenarios, we often encounter various data formats. The most common one is JSON data format, followed by XML, YAML, and CSV formats. All of these require us to have some ability to parse and transform their data formats. In this article, I will teach you how to handle JSON and XML data formats.
JSON format conversion in node-red
Handling JSON data format in node-red is the easiest. The main reason is that node-red is itself written in JavaScript, and JSON data format is the most commonly used in JavaScript. Declaring a JSON data format is very simple.
# json 数据的声明
const person = {name: 'fizzz', age:12}
# 打印数据
console.log(person.name)
# 重新赋值
person.name = 'tom'
The operation of JSON data format is just as simple.
Below is a flow for JSON conversion, through which you can experience the operation of JSON data.
[{"id":"634256b7.2d6818","type":"inject","z":"64133d39.bb0394","name":"JSON String","topic":"","payload":"{"a":1}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":80,"wires":[["a2fe0fc8.095e1"]]},{"id":"a2fe0fc8.095e1","type":"json","z":"64133d39.bb0394","name":"","property":"payload","action":"","pretty":false,"x":270,"y":80,"wires":[["9a4ce2b8.47698"]]},{"id":"9a4ce2b8.47698","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":80,"wires":[]},{"id":"80032e2.7c92cd","type":"inject","z":"64133d39.bb0394","name":"Object","topic":"","payload":"{"a":1}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":120,"wires":[["cd40a0f4.4f5ac"]]},{"id":"cd40a0f4.4f5ac","type":"json","z":"64133d39.bb0394","name":"","property":"payload","action":"","pretty":false,"x":270,"y":120,"wires":[["478b4106.4fd7c"]]},{"id":"478b4106.4fd7c","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":120,"wires":[]}]
The effect diagram is as follows:
JSON data processing mainly relies on the core JSON node, which can convert strings and JSON data to each other. It is very convenient. If the conversion fails, an error will be reported.
The detailed configuration of the JSON node is as follows:
Input parameters:payload: object | string
JavaScript object or JSON string.schema: object
An optional JSON Schema object used to validate the payload. This property will be removed before sending the msg to the next node.
Output parameter:payload: object | string
If the input is a JSON string, it will attempt to parse it into a JavaScript object.
If the input is a JavaScript object, it will create a JSON string. And you can choose to reshape this JSON string.schema: Error array
If the JSON schema validation fails, the catch node will have a schemaError property containing an array of errors.
Processing xml-formatted data in node-red
Processing xml data in node-red, the main scenario is converting json data to xml data, or converting xml-formatted data to json data format. The main tool used is the xml core node, which uses the xml2json library at the bottom.
This xml node requires two input parameters
payload: object | string
Set as a JavaScript object or XML string.
options:object
You can pass options to the internally used XML conversion library. Please refer to the xml2js documentation for more information.
The output result ispayload: object | string
If the input is a string, it will attempt to parse it as XML and create a JavaScript object.
If the input is a JavaScript object, it will attempt to build an XML string.
Additionally, when converting between XML and objects, by default, XML attributes are added to a property named $, and text content is added to a property named _. These property names can be changed in the node settings.
Below is an example where you can feel the conversion of xml data to json data format by node-red
[{"id":"1b546d47.9474e3","type":"inject","z":"64133d39.bb0394","name":"XML String","topic":"","payload":"{"a":1}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":100,"y":260,"wires":[["d72b2bfd.77d068"]]},{"id":"1adf407d.6c4fe","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":260,"wires":[]},{"id":"46638890.8ae758","type":"inject","z":"64133d39.bb0394","name":"Object","topic":"","payload":"{"note":{"$":{"priority":"high"},"to":["Nick"],"from":["Dave"],"heading":["Reminder"],"body":["Update the website"]}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":300,"wires":[["dae1d291.de0d2"]]},{"id":"6fefca67.3669e4","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":300,"wires":[]},{"id":"d72b2bfd.77d068","type":"template","z":"64133d39.bb0394","name":"","field":"payload","fieldType":"msg","format":"text","syntax":"plain","template":"<note priority="high">n <to>Nick</to>n <from>Dave</from>n <heading>Reminder</heading>n <body>Update the website</body>n</note>","output":"str","x":280,"y":260,"wires":[["1746464a.87aa4a"]]},{"id":"1746464a.87aa4a","type":"xml","z":"64133d39.bb0394","name":"","property":"payload","attr":"","chr":"","x":430,"y":260,"wires":[["1adf407d.6c4fe"]]},{"id":"dae1d291.de0d2","type":"xml","z":"64133d39.bb0394","name":"","property":"payload","attr":"","chr":"","x":250,"y":300,"wires":[["6fefca67.3669e4"]]}]
Flow diagram
Leave a Reply