Change Payload include Array

My payload is like this
{
"deviceId":"B42EB8",
"time":"2018-07-30T14:46:31.872257Z",
"_msgid":"bca8ce3a.2962b",
"seq":"",
"dataView":"",
"enters":9866,
"exits":11165,
"type":"string",
"unit":"",
"recep":""
"id":"",
"MEE":"",
"TWS":0,
}

I would like with this format
{
"deviceId": "B42EB8",
"time": "2018-07-30T14:46:31.872257Z",
"seq": "",
"dataView": [
{
"key": "enters",
"value": 9866,
"type": "string",
"unit": "",
},
{
"key": "exits",
"value": 11165,
"type": "string",
"unit": "",
}
],
"recep": [
{
"id": "",
"MEE": 0,
"TWS": 0
}
]
}

Please, someone could help me

Below flow is not quite creative but it apparently does the job. Of course you will use only the second function node to transform your original object.

[{"id":"6bc749e3.bd1c78","type":"tab","label":"Flow 1","disabled":false,"info":""},{"id":"ce09602b.03d27","type":"inject","z":"6bc749e3.bd1c78","name":"","topic":"","payload":"","payloadType":"date","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":160,"wires":[["fda2330d.8f348"]]},{"id":"fda2330d.8f348","type":"function","z":"6bc749e3.bd1c78","name":"Original data","func":"msg.payload = {\n\"deviceId\":\"B42EB8\",\n\"time\":\"2018-07-30T14:46:31.872257Z\",\n\"_msgid\":\"bca8ce3a.2962b\",\n\"seq\":\"\",\n\"dataView\":\"\",\n\"enters\":9866,\n\"exits\":11165,\n\"type\":\"string\",\n\"unit\":\"\",\n\"recep\":\"\",\n\"id\":\"\",\n\"MEE\":\"\",\n\"TWS\":0,\n};\n\nreturn msg;","outputs":1,"noerr":0,"x":340,"y":160,"wires":[["8e233d10.9df15"]]},{"id":"8e233d10.9df15","type":"function","z":"6bc749e3.bd1c78","name":"Transform original data","func":"let deviceId = msg.payload.deviceId;\nlet time = msg.payload.time;\nlet seq = msg.payload.seq;\n\nlet recep = {\n  \"id\":msg.payload.id,\n  \"MEE\":msg.payload.MEE,\n  \"TWS\":msg.payload.TWS\n};\n\nlet enters = {\n    \"key\":\"enters\",\n    \"value\": msg.payload.enters,\n    \"type\": msg.payload.type,\n    \"unit\": msg.payload.unit\n};\n\nlet exits = {\n    \"key\":\"exits\",\n    \"value\": msg.payload.exits,\n    \"type\": msg.payload.type,\n    \"unit\": msg.payload.unit\n};\n\n\nmsg.payload = {\n    \"deviceID\": deviceId, \n    \"time\":time, \n    \"seq\":seq,\n    \"dataView\": [enters,exits],\n    \"recep\": [recep]\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":540,"y":160,"wires":[["597e7e14.9919b"]]},{"id":"597e7e14.9919b","type":"debug","z":"6bc749e3.bd1c78","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":730,"y":160,"wires":[]}]

Thank you very much @Andrei you saved my life

1 Like