数据格式化-CSV处理
从包含键/值对数据的消息生成有效的 CSV 输出。
问题背景
从包含键/值对数据的消息生成有效的 CSV 输出。
解决方案
CSV 处理 节点可用于生成格式良好的 CSV 字符串。
示例

示例JSON
[{"id":"457d9ad6.b737b4","type":"inject","z":"4dcd5b540cc77711","name":"single","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":560,"wires":[["1e05fafd.887b05"]]},{"id":"1e05fafd.887b05","type":"change","z":"4dcd5b540cc77711","name":"生成随机数对象","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":360,"y":560,"wires":[["e9546682.b39898","1c0576ca87abd217"]]},{"id":"e9546682.b39898","type":"csv","z":"4dcd5b540cc77711","name":"","sep":",","hdrin":"","hdrout":"none","multi":"one","ret":"\\n","temp":"a,b,c","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":630,"y":560,"wires":[["f83ad3b0.78d32"]]},{"id":"f83ad3b0.78d32","type":"debug","z":"4dcd5b540cc77711","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":560,"wires":[]},{"id":"ae242f2c.d1c8a","type":"inject","z":"4dcd5b540cc77711","name":"array","repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":210,"y":620,"wires":[["7535f521.4a88bc"]]},{"id":"7535f521.4a88bc","type":"change","z":"4dcd5b540cc77711","name":"生成随机数对象数组","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":380,"y":620,"wires":[["f4e0465f.ef0338","4a58ee9f647dde3a"]]},{"id":"f4e0465f.ef0338","type":"csv","z":"4dcd5b540cc77711","name":"","sep":",","hdrin":"","hdrout":"all","multi":"one","ret":"\\n","temp":"a,b,c","skip":"0","strings":true,"include_empty_strings":false,"include_null_values":false,"x":630,"y":620,"wires":[["6eb67fdf.58626"]]},{"id":"6eb67fdf.58626","type":"debug","z":"4dcd5b540cc77711","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","x":770,"y":620,"wires":[]},{"id":"1c0576ca87abd217","type":"debug","z":"4dcd5b540cc77711","logName":"日志输出","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","logLevel":"info","dataType":"field","format":"handlebars","syntax":"mustache","template":"输出日志: payload 值为 {{payload}}","output":"str","x":590,"y":500,"wires":[]},{"id":"4a58ee9f647dde3a","type":"debug","z":"4dcd5b540cc77711","logName":"日志输出","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","logLevel":"info","dataType":"field","format":"handlebars","syntax":"mustache","template":"输出日志: payload 值为 {{payload}}","output":"str","x":610,"y":740,"wires":[]}]
运行结果
在示例中,第一个流注入了一个有效负载,该有效负载包含一个具有三个包含随机生成的值的属性的单个对象。
{
a: 10,
b: 20,
c: 30
}
CSV 处理 节点已配置所需的列名,并使用相应的对象属性来填充这些列。生成的消息包含该单行数据的格式正确的 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
