前言

在前面的章节中我介绍了如何在Node-RED中处理json与xml格式的数据。但随后又有读者向我询问,
在node-red中如何处理yaml格式的数据,以及csv格式的数据。yaml格式的数据是随着这几年云原生而大行其道的数据格式。因为不需要中括号,格式优雅,而收到开发人员的喜爱。云原生中的资源定义都可以使用yaml文件来定义。CSV格式是实施分隔的文本文件。可以与excel中的数据互相转化。

YAML格式的数据处理

处理yaml格式数据主要使用的是yaml节点。该节点可以实现 在YAML格式的字符串及其JavaScript对象即json数据格式之间相互转换。

输入:
payload object | 字符串
JavaScript 对象或YAML字符串。

输出:
payload object | 字符串
● 如果输入是YAML字符串,它将尝试将其解析为JavaScript对象。
● 如果输入是JavaScript对象,它将创建一个YAML字符串。

以下是一个示例:
可以将yaml的字符串转换为json对象

如字符串变量 {"a":1, "b":[1,2,3]} 转换成yaml 就变成了

a: 1
b: 
  - 1
  - 2
  - 3
在node-red中处理yaml,csv格式的数据格式插图1

完整的数据流

[{"id":"f231967.0251a68","type":"inject","z":"64133d39.bb0394","name":"YAML String","topic":"","payload":"{\"a\":1}","payloadType":"str","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":110,"y":320,"wires":[["a0110756.ecfa48"]]},
{"id":"8f8f31b7.1f916","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":320,"wires":[]},
{"id":"5138ba3.c972444","type":"inject","z":"64133d39.bb0394","name":"Object","topic":"","payload":"{\"a\":1, \"b\":[1,2,3]}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":360,"wires":[["2fa653cc.60d3dc"]]},
{"id":"50f2f4c.4a6e60c","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":430,"y":360,"wires":[]},
{"id":"a0110756.ecfa48","type":"template","z":"64133d39.bb0394","name":"","field":"payload","fieldType":"msg","format":"yaml","syntax":"plain","template":"a: 1\nb:\n  - 1\n  - 2\n  - 3","output":"str","x":280,"y":320,"wires":[["104b80e2.51068f"]]},
{"id":"2fa653cc.60d3dc","type":"yaml","z":"64133d39.bb0394","property":"payload","name":"","x":250,"y":360,"wires":[["50f2f4c.4a6e60c"]]},
{"id":"104b80e2.51068f","type":"yaml","z":"64133d39.bb0394","property":"payload","name":"","x":430,"y":320,"wires":[["8f8f31b7.1f916"]]}]

CSV格式的数据处理

对于CSV格式,一种是要输出CSV格式的数据,如,每行以逗号隔开。CSV格式的数据处理离不开node-red自带的csv节点。

在CSV格式的字符串及其JavaScript对象表示形式之间进行相互转换。

输入:

payload object | 数组 | 字符串

JavaScript对象,数组或CSV字符串。

输出:

payload object | 数组 | 字符串

  • 如果输入是字符串,它将尝试将其解析为CSV,并为每行创建键/值对的JavaScript对象。然后该节点将为每行发送一条消息,或者发送一条包含对象数组的消息。
  • 如果输入是JavaScript对象,它将尝试构建CSV字符串。
  • 如果输入是简单值的数组,则将构建单行CSV字符串。
  • 如果输入是数组数组或对象数组,则会创建多行CSV字符串。

列模板可以包含列名称的有序列表。将CSV转换为对象时,列名将用作属性名称。或者也可以从CSV的第一行中获取列名称。

转换为CSV时,列模板用于标识从对象中提取的属性以及提取的顺序。

如果输入是数组,则列模板仅用于有选择地生成一行列标题。

只要正确设置parts属性,该节点就可以接受多部分输入。

如果输出多个消息,则将设置其parts属性并形成完整的消息序列。

注意:列模板必须用逗号分隔,即使数据中已有了其他分隔符。

CSV 节点的配置页面

在node-red中处理yaml,csv格式的数据格式插图3

csv节点可以将json数据格式,转为为字符串。

{
   a: 10,
   b: 20,
   c: 30
}

通过CSV可以转化为"10,20,30\n"
你也可以通过它来转化数组
如下面的数组,

[
    { a: 80, b: 18, c: 2},
    { a: 52, b: 36, c: 10},
    { a: 91, b: 18, c: 61},
    { a: 32, b: 47, c: 65},
]

通过CSV可以转化为

a,b,c
80,18,2
52,36,10
91,18,61
32,47,65

完整的例子流代码

[{"id":"457d9ad6.b737b4","type":"inject","z":"64133d39.bb0394","name":"single","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":640,"wires":[["1e05fafd.887b05"]]},{"id":"1e05fafd.887b05","type":"change","z":"64133d39.bb0394","name":"Generate single payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"{ \"a\":$floor(100*$random()),\"b\":$floor(100*$random()),\"c\":$floor(100*$random())}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":640,"wires":[["e9546682.b39898"]]},
{"id":"e9546682.b39898","type":"csv","z":"64133d39.bb0394","name":"","sep":",","hdrin":"","hdrout":false,"multi":"one","ret":"\\n","temp":"a,b,c","skip":"0","x":450,"y":640,"wires":[["f83ad3b0.78d32"]]},
{"id":"f83ad3b0.78d32","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":640,"wires":[]},
{"id":"ae242f2c.d1c8a","type":"inject","z":"64133d39.bb0394","name":"array","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":90,"y":700,"wires":[["7535f521.4a88bc"]]},
{"id":"7535f521.4a88bc","type":"change","z":"64133d39.bb0394","name":"Generate array payload","rules":[{"t":"set","p":"payload","pt":"msg","to":"[\t    { \"a\":$floor(100*$random()),\"b\":$floor(100*$random()),\"c\":$floor(100*$random())},\t    { \"a\":$floor(100*$random()),\"b\":$floor(100*$random()),\"c\":$floor(100*$random())},\t    { \"a\":$floor(100*$random()),\"b\":$floor(100*$random()),\"c\":$floor(100*$random())},\t    { \"a\":$floor(100*$random()),\"b\":$floor(100*$random()),\"c\":$floor(100*$random())}\t]","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":270,"y":700,"wires":[["f4e0465f.ef0338"]]},
{"id":"f4e0465f.ef0338","type":"csv","z":"64133d39.bb0394","name":"","sep":",","hdrin":"","hdrout":true,"multi":"one","ret":"\\n","temp":"a,b,c","skip":"0","x":450,"y":700,"wires":[["6eb67fdf.58626"]]},
{"id":"6eb67fdf.58626","type":"debug","z":"64133d39.bb0394","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":590,"y":700,"wires":[]}]

csv转json

除此之外CSV节点还可以将CSV格式的数据 进行转化,转成成JSON数据。
这是一个CSV格式的数据

a,b,c
80,18,2
52,36,10
91,18,61
32,47,65

通过CSV节点,它可以转化为标准的JSON格式

在node-red中处理yaml,csv格式的数据格式插图5
[
    { a: 80, b: 18, c: 2},
    { a: 52, b: 36, c: 10},
    { a: 91, b: 18, c: 61},
    { a: 32, b: 47, c: 65},
]

相关新闻

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注

邮箱

cloud@modbus.cn

QQ
QQ
微信
微信
分享本页
返回顶部