# Read json and extract id from it

**URL:** <https://discourse.nodered.org/t/read-json-and-extract-id-from-it/12092>\
**Category:** General\
**Created:** [12 June 2019 07:29 UTC](https://discourse.nodered.org/t/read-json-and-extract-id-from-it/12092 "2019-06-12T07:29:57Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [12 June 2019 07:29 UTC](https://discourse.nodered.org/t/read-json-and-extract-id-from-it/12092/1 "2019-06-12T07:29:57Z")

</div>

Hi, i need some help!  
and I want to select the idx where name equal as the name received by a post (msg.name)  
Is that possible?

In node red i send a json query to my domoticz  
i execute [http://192.168.20.35:82/json.htm?type=command&param=getlightswitches](http://192.168.20.35:82/json.htm?type=command&param=getlightswitches)  
and i get the following output:  
I need an idx for a specifica switch name received by a post, for example 'Lantaarn' and need the idx of it. next step will be to turn idx xxx on (that part already works)

{  
"result" : [  
{  
"DimmerLevels" : "none",  
"IsDimmer" : false,  
"Name" : "voice\_vorstalarm",  
"SubType" : "Switch",  
"Type" : "Light/Switch",  
"idx" : "1163"  
},  
{  
"DimmerLevels" : "none",  
"IsDimmer" : false,  
"Name" : "voice\_wasmachine",  
"SubType" : "Switch",  
"Type" : "Light/Switch",  
"idx" : "1149"  
},  
{  
"DimmerLevels" : "none",  
"IsDimmer" : false,  
"Name" : "voordeurbel",  
"SubType" : "Switch",  
"Type" : "Light/Switch",  
"idx" : "1182"  
},  
{  
"DimmerLevels" : "none",  
"IsDimmer" : false,  
"Name" : "vriezer",  
"SubType" : "Switch",  
"Type" : "Light/Switch",  
"idx" : "469"  
},  
{  
"DimmerLevels" : "none",  
"IsDimmer" : false,  
"Name" : "wasmachine",  
"SubType" : "Switch",  
"Type" : "Light/Switch",  
"idx" : "976"  
},  
{  
"DimmerLevels" : "none",  
"IsDimmer" : false,  
"Name" : "wii\_lan\_ip",  
"SubType" : "AC",  
"Type" : "Lighting 2",  
"idx" : "288"  
}  
],  
"status" : "OK",  
"title" : "GetLightSwitches"  
}

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [12 June 2019 08:31 UTC](https://discourse.nodered.org/t/read-json-and-extract-id-from-it/12092/2 "2019-06-12T08:31:03Z")

</div>

as msg.payload.result is an array of objects, you could use `Array.find()` prototype.

Here is an example...

```auto
[{"id":"ff8f07f0.a4d518","type":"inject","z":"ade1a96c.062848","name":"Emulate data source","topic":"","payload":"{ \"result\" : [{ \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voice_vorstalarm\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1163\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voice_wasmachine\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1149\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voordeurbel\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1182\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"vriezer\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"469\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"wasmachine\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"976\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"wii_lan_ip\", \"SubType\" : \"AC\", \"Type\" : \"Lighting 2\", \"idx\" : \"288\" }], \"status\" : \"OK\", \"title\" : \"GetLightSwitches\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":762,"y":120,"wires":[["eb5252e1.cb8a1"]]},{"id":"e4e6698a.662c58","type":"debug","z":"ade1a96c.062848","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1226,"y":168,"wires":[]},{"id":"9b19d12b.8b225","type":"function","z":"ade1a96c.062848","name":"find","func":"let arr = msg.payload.result;\nlet find = msg.find;\nlet found = arr.find(function(o,i){\n return o.Name == find\n})\nif(found){\n msg.payload = found\n return [msg,null];\n}\n\nnode.warn(`could not find '${msg.find}'`);","outputs":1,"noerr":0,"x":1202,"y":120,"wires":[["e4e6698a.662c58"]]},{"id":"eb5252e1.cb8a1","type":"change","z":"ade1a96c.062848","name":"find voice_vorstalarm","rules":[{"t":"set","p":"find","pt":"msg","to":"voice_vorstalarm","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":988,"y":120,"wires":[["9b19d12b.8b225"]]},{"id":"be97cb5b.1448d8","type":"inject","z":"ade1a96c.062848","name":"Emulate data source","topic":"","payload":"{ \"result\" : [{ \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voice_vorstalarm\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1163\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voice_wasmachine\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1149\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voordeurbel\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1182\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"vriezer\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"469\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"wasmachine\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"976\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"wii_lan_ip\", \"SubType\" : \"AC\", \"Type\" : \"Lighting 2\", \"idx\" : \"288\" }], \"status\" : \"OK\", \"title\" : \"GetLightSwitches\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":762,"y":72,"wires":[["b296fea1.9a217"]]},{"id":"b296fea1.9a217","type":"change","z":"ade1a96c.062848","name":"find Lantaarn","rules":[{"t":"set","p":"find","pt":"msg","to":"Lantaarn","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":958,"y":72,"wires":[["9b19d12b.8b225"]]},{"id":"e2330b3e.b54a18","type":"inject","z":"ade1a96c.062848","name":"Emulate data source","topic":"","payload":"{ \"result\" : [{ \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voice_vorstalarm\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1163\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voice_wasmachine\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1149\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"voordeurbel\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"1182\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"vriezer\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"469\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"wasmachine\", \"SubType\" : \"Switch\", \"Type\" : \"Light/Switch\", \"idx\" : \"976\" }, { \"DimmerLevels\" : \"none\", \"IsDimmer\" : false, \"Name\" : \"wii_lan_ip\", \"SubType\" : \"AC\", \"Type\" : \"Lighting 2\", \"idx\" : \"288\" }], \"status\" : \"OK\", \"title\" : \"GetLightSwitches\" }","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":762,"y":168,"wires":[["24406a14.15c486"]]},{"id":"24406a14.15c486","type":"change","z":"ade1a96c.062848","name":"find voordeurbel","rules":[{"t":"set","p":"find","pt":"msg","to":"voordeurbel","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":968,"y":168,"wires":[["9b19d12b.8b225"]]}]

```

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/2X/9/902d331880932e8a0212073d664f77b7fca2a870.png)

---

<div class="post-metadata">

**Author:** ![pvanklink](https://avatars.discourse-cdn.com/v4/letter/p/bcef8e/32.png) [@pvanklink](https://discourse.nodered.org/u/pvanklink)\
**Post date:** [12 June 2019 09:34 UTC](https://discourse.nodered.org/t/read-json-and-extract-id-from-it/12092/3 "2019-06-12T09:34:57Z")

</div>

Thanks, After weaks of trying finaly a beginning !

I still need some help:

1. in find emulate name i need the value of name from the data source of from the post
2. i think i loose some info because my switch does not fires. my mqtt works ok

> [{"id":"ce203651.101258","type":"tab","label":"Domoticz WatchDog","disabled":false,"info":""},{"id":"ea662623.d14a98","type":"http request","z":"ce203651.101258","name":"test return","method":"GET","ret":"obj","paytoqs":false,"url":"[http://192.168.20.35:82/json.htm?type=command&param=getlightswitches","tls":"","proxy":"","x":380,"y":120,"wires":[["374e028a.06636e"]]},{"id":"d59c0ea7.99a59","type":"debug","z":"ce203651.101258","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1230,"y":60,"wires":[]},{"id":"b65848b2.bcd588","type":"function","z":"ce203651.101258","name":"find","func":"let](http://192.168.20.35:82/json.htm?type=command&param=getlightswitches%22,%22tls%22:%22%22,%22proxy%22:%22%22,%22x%22:380,%22y%22:120,%22wires%22:%5B%5B%22374e028a.06636e%22%5D%5D%7D,%7B%22id%22:%22d59c0ea7.99a59%22,%22type%22:%22debug%22,%22z%22:%22ce203651.101258%22,%22name%22:%22%22,%22active%22:true,%22tosidebar%22:true,%22console%22:false,%22tostatus%22:false,%22complete%22:%22true%22,%22targetType%22:%22full%22,%22x%22:1230,%22y%22:60,%22wires%22:%5B%5D%7D,%7B%22id%22:%22b65848b2.bcd588%22,%22type%22:%22function%22,%22z%22:%22ce203651.101258%22,%22name%22:%22find%22,%22func%22:%22let) arr = msg.payload.result;\nlet find = msg.find;\nlet found = arr.find(function(o,i){\n return o.Name == find\n})\nif(found){\n msg.payload = found\n return [msg,null];\n}\n\nnode.warn(`Switch met opgegeven naam niet gevonden '${msg.find}'`);","outputs":1,"noerr":0,"x":350,"y":420,"wires":[["994f245.4f6e2d8"]]},{"id":"374e028a.06636e","type":"change","z":"ce203651.101258","name":"find emulate name","rules":[{"t":"set","p":"find","pt":"msg","to":"lantaarn","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":670,"y":120,"wires":[["b65848b2.bcd588"]]},{"id":"994f245.4f6e2d8","type":"change","z":"ce203651.101258","name":"maak switch bericht","rules":[{"t":"set","p":"command","pt":"msg","to":"payload.command","tot":"msg"},{"t":"set","p":"idx","pt":"msg","to":"payload.idx","tot":"msg"},{"t":"set","p":"switchcmd","pt":"msg","to":"payload.switchcmd","tot":"msg"},{"t":"set","p":"level","pt":"msg","to":"payload.level","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":710,"y":420,"wires":[["20d4d014.de57c","d59c0ea7.99a59"]]},{"id":"20d4d014.de57c","type":"mqtt out","z":"ce203651.101258","name":"","topic":"domoticz/in","qos":"","retain":"","broker":"21fd69bf.668b46","x":1130,"y":420,"wires":},{"id":"2139ca6d.abdcc6","type":"comment","z":"ce203651.101258","name":"This is the payload i get from IFTT webhook POST BODY","info":"","x":230,"y":60,"wires":},{"id":"854061ae.dd1ea","type":"comment","z":"ce203651.101258","name":"domotica only switch idx to on does not now name","info":"","x":770,"y":340,"wires":},{"id":"ddf8783b.ecde78","type":"http in","z":"ce203651.101258","name":"LIVE met post","url":"/pvkapi2","method":"post","upload":false,"swaggerDoc":"","x":130,"y":120,"wires":[["ea662623.d14a98"]]},{"id":"9abdda3a.34b798","type":"comment","z":"ce203651.101258","name":"body content","info":"{"command":"switchlight","name":"lantaarn","switchcmd":"On"}","x":530,"y":60,"wires":},{"id":"3a61696f.685806","type":"inject","z":"ce203651.101258","name":"Emulate data source","topic":"","payload":"{"command":"switchlight","name":"lantaarn","switchcmd":"On"}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":130,"y":200,"wires":[["ea662623.d14a98"]]},{"id":"21fd69bf.668b46","type":"mqtt-broker","z":"","name":"mqttdomoticz","broker":"localhost","port":"1883","clientid":"","usetls":false,"compatmode":true,"keepalive":"60","cleansession":true,"birthTopic":"","birthQos":"0","birthPayload":"","closeTopic":"","closeQos":"0","closePayload":"","willTopic":"","willQos":"0","willPayload":""}]
