Represent a JSON in a table and create a CSV file

Hello. I currently have a JSON that has the following structure:

mobj={1356895650000:{temp1:23, temp2:25,temp3:45,temp4:12},
1356895680000:{temp1:14, temp2:24,temp3:25,temp4:16},
1356895710000:{temp1:13, temp2:22,temp3:15,temp4:28},
....}

what I need is to represent this in a table in the following way:

Time /temp1 /temp2 /temp3 /temp4
1356895650000 23 25 45 12
1356896580000 14 24 25 16
1356895710000 13 22 15 28

Also, I want to export this data in CSV format, being able to visualize the data in the same way.
Right now I'm using the CSV node, but I'm obtaining this

You need to create an array of objects.
Something like this.

[{"id":"d50e160a0f210ab9","type":"inject","z":"bf9e1e33.030598","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":"","topic":"","payload":"{\"1356895650000\":{\"temp1\":23,\"temp2\":25,\"temp3\":45,\"temp4\":12},\"1356895680000\":{\"temp1\":14,\"temp2\":24,\"temp3\":25,\"temp4\":16}}","payloadType":"json","x":110,"y":1880,"wires":[["75662bbb.02ab1c"]]},{"id":"75662bbb.02ab1c","type":"change","z":"bf9e1e33.030598","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$each($$.payload, function ($v, $k){\t    $merge([{\"Time\": $k}, $v])\t})","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":320,"y":1880,"wires":[["7dc1e1fa.49b8b8","3551e8a9.cc3b6"]]},{"id":"7dc1e1fa.49b8b8","type":"csv","z":"bf9e1e33.030598","name":"","sep":",","hdrin":"","hdrout":"all","multi":"one","ret":"\\n","temp":"","skip":"0","strings":true,"include_empty_strings":"","include_null_values":"","x":530,"y":1880,"wires":[["6160bb0b377e4cac"]]},{"id":"3551e8a9.cc3b6","type":"ui_table","z":"bf9e1e33.030598","group":"8b5cde76.edd58","name":"","order":2,"width":0,"height":0,"columns":[],"outputs":0,"cts":false,"x":530,"y":1940,"wires":[]},{"id":"6160bb0b377e4cac","type":"debug","z":"bf9e1e33.030598","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":660,"y":1880,"wires":[]},{"id":"8b5cde76.edd58","type":"ui_group","name":"default","tab":"8f03e639.85956","order":1,"disp":false,"width":"12","collapse":false},{"id":"8f03e639.85956","type":"ui_tab","name":"Home","icon":"dashboard","order":2,"disabled":false,"hidden":false}]

You can then feed the array into the csv node and ui-table node.

3 Likes

Thank you for your answer, it was the solution! I make the same with a function node but your way is faster!!! Maybe do you know how can I represente the timestamp as "human date"?

$each($$.payload, function ($v, $k){
       $merge([{"Time": $fromMillis($number($k))}, $v])
})

or

$each($$.payload, function ($v, $k){
       $merge([{"Time": $moment($number($k)).format("DD/MM/yyyy HH:mm:ss")}, $v])
})
2 Likes

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.