前言
在使用node-red处理各种业务场景时,我们时常会遇到各种各样的数据格式,最常见的是json数据格式,其次还有xml,yaml,以及csv格式的数据格式。这些都是需要我们对其数据格式有一点解析和转化的能力,今天这篇文章我就来教大家如何处理json 和 xml数据格式。
node-red中json格式的转换
在node-red中处理json数据格式是最简单的。最主要的原因是node-red本身就是使用javascript语言来编写的。而javascript中最常用的就是json数据格式。声明一个json数据格式很简单。
# json 数据的声明
const person = {name: 'fizzz', age:12}
# 打印数据
console.log(person.name)
# 重新赋值
person.name = 'tom'
json 数据格式的操作就是这样的简单。
下面是一个json转换的流,大家可以从这个流中体验的对json数据的操作。
[{"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":[]}]
效果图如下:
json 数据的处理主要是借助来json核心节点,它可以将字符串与json数据互转。 非常方便。如果转换失败就会报错。
json 节点的详细配置如下:
输入参数:payload
:object | 字符串
JavaScript对象或JSON字符串。schema
:object
可选的JSON Schema对象用于验证有效负载。在将msg发送到下一个节点之前,将删除该属性。
输出参数:payload
:object | 字符串
如果输入是JSON字符串,它将尝试将其解析为JavaScript对象。
如果输入是JavaScript对象,它将创建一个JSON字符串。并可以选择对此JSON字符串进行整形。schema
:Error数组
如果JSON模式验证失败,则catch节点将具有包含错误数组的schemaError属性。
node-red中处理 xml格式的数据
在ndoe-red中处理xml的数据,主要场景是 json数据转换为xml数据,或者xml格式的数据转换为json数据格式。其中主要借助的是xml核心节点,其底层是使用了xml2json这个库。
xml这个节点的需要的输入有二个参数
payload
: object | 字符串
设置为JavaScript对象或XML字符串。
options
:object
可以将选项传递给内部使用的XML转换库。请参见 xml2js文档 来获取更多信息。
输出结果是payload
: object | 字符串
如果输入是字符串,它将尝试将其解析为XML并创建一个JavaScript对象。
如果输入是JavaScript对象,它将尝试构建XML字符串。
另外在XML和对象之间进行转换时,默认情况下XML属性会添加到名为$的属性中。将文本内容添加到名为_的属性中。这些属性名称可以在节点设置中更改。
下面是一个例子,大家可以从这个例子感受到node-red对xml数据与json数据格式的转换
[{"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"]]}]
流示意图