How to access data from a JSON file using a specific key

form{
"id": "0002",
"data": {
"data_type": "cmd_v3",
"id": "0002",
"n": "fcu_01",
"function": "write",
"item": [
{
"n": "power",
"data": "v"
},
{
"n": "set_temp",
"data": v
},
{
"n": "fan_speed",
"data": "v"
},
{
"n": "mode",
"data": "v"
}
]
}
}

i wat to access data like
n: set_temp data = "v"
n : fan_speed data = "v"
n: mode data = "v"
n : power data = "v"
how can i make this without calling through the array position. Thank

You could reform the data to an object
then call the data msg.payload.set_temp
e.g.

[{"id":"474599d714e96bb9","type":"inject","z":"1d319bc3005a51a2","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"form\":{\"id\":\"0002\",\"data\":{\"data_type\":\"cmd_v3\",\"id\":\"0002\",\"n\":\"fcu_01\",\"function\":\"write\",\"item\":[{\"n\":\"power\",\"data\":\"v\"},{\"n\":\"set_temp\",\"data\":\"v\"},{\"n\":\"fan_speed\",\"data\":\"v\"},{\"n\":\"mode\",\"data\":\"v\"}]}}}","payloadType":"json","x":130,"y":280,"wires":[["4aaf250378451bf5"]]},{"id":"4aaf250378451bf5","type":"change","z":"1d319bc3005a51a2","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload.form.data.item.${$.n:$.data}","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":300,"y":280,"wires":[["3b64295684b6c8c2"]]},{"id":"3b64295684b6c8c2","type":"debug","z":"1d319bc3005a51a2","name":"debug 4 flow 3","active":true,"tosidebar":true,"console":true,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":530,"y":280,"wires":[]}]

Output

{ 
  "power":"v",
  "set_temp":"v",
  "fan_speed":"v",
  "mode":"v"
}
2 Likes

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