Extract object name and put it in a flow variable (help needed)


#1

I am trying to setup a router from Node-Red dashboard via JSON-RPC, payload response received from router is a JSON object:
{"_msgid":"124cf3d6.b47a9c","topic":"","payload":{"jsonrpc":"2.0","id":6,"result":[0,{"values":{"cfg023579":{".anonymous":true,".type":"wifi-iface",".name":"cfg023579",".index":1,"device":"radio0","network":"lan","mode":"ap","isolate":"0","user_enable":"1","hotspotid":"hotspot1","ssid":"HOOS","key":"development","ttl_increase":"0","encryption":"psk-mixed"}}}]},"statusCode":200,"responseUrl":"http://192.168.200.2/ubus","headers":{"connection":"close","transfer-encoding":"chunked","x-frame-options":"SAMEORIGIN","content-type":"application/json","x-node-red-request-node":"3156b968"}}

From this payload I want to extract the name of the object "cfg023579" which is dynamic and put it in a flow variable:

After this, to acces the values I need to use the flow variable:


#2

I found a way to extract the object name and put it in a flow variable, but how I include the value of the variable in the payload path?

msgSSID.payload = msg.payload.result[1].values.cfg023579.ssid;

cfg023579 to be variable value...


#3

jsonata is a good solution for this use case.

For instance, the expression: $keys($.payload.result.values) applied to your payload will result in an array of keys

[
  "cfg023579"
]

Probably you will need to modify a little below flow to suit your payload:

[{"id":"b341cefe.91247","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"9eb950ef.2adea","type":"inject","z":"b341cefe.91247","name":"Trigger","topic":"","payload":"{\"_msgid\":\"124cf3d6.b47a9c\",\"topic\":\"\",\"payload\":{\"jsonrpc\":\"2.0\",\"id\":6,\"result\":[0,{\"values\":{\"cfg023579\":{\".anonymous\":true,\".type\":\"wifi-iface\",\".name\":\"cfg023579\",\".index\":1,\"device\":\"radio0\",\"network\":\"lan\",\"mode\":\"ap\",\"isolate\":\"0\",\"user_enable\":\"1\",\"hotspotid\":\"hotspot1\",\"ssid\":\"HOOS\",\"key\":\"development\",\"ttl_increase\":\"0\",\"encryption\":\"psk-mixed\"}}}]},\"statusCode\":200,\"responseUrl\":\"http://192.168.200.2/ubus\",\"headers\":{\"connection\":\"close\",\"transfer-encoding\":\"chunked\",\"x-frame-options\":\"SAMEORIGIN\",\"content-type\":\"application/json\",\"x-node-red-request-node\":\"3156b968\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":300,"wires":[["e1127e10.612fb","4339c31.5b0e93c"]]},{"id":"c119f43.dd8c308","type":"debug","z":"b341cefe.91247","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":610,"y":300,"wires":[]},{"id":"e1127e10.612fb","type":"change","z":"b341cefe.91247","name":"","rules":[{"t":"set","p":"result","pt":"msg","to":"$keys($.payload.payload.result.values)","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":300,"wires":[["c119f43.dd8c308"]]},{"id":"4339c31.5b0e93c","type":"debug","z":"b341cefe.91247","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","x":430,"y":240,"wires":[]}]

Now, if the ultimate goal is to extract the SSID from the object then the jsonata expression would be as simple as **.ssid, resulting in "HOOS"

Flow:

[{"id":"b341cefe.91247","type":"tab","label":"Flow 3","disabled":false,"info":""},{"id":"9eb950ef.2adea","type":"inject","z":"b341cefe.91247","name":"Trigger","topic":"","payload":"{\"_msgid\":\"124cf3d6.b47a9c\",\"topic\":\"\",\"payload\":{\"jsonrpc\":\"2.0\",\"id\":6,\"result\":[0,{\"values\":{\"cfg023579\":{\".anonymous\":true,\".type\":\"wifi-iface\",\".name\":\"cfg023579\",\".index\":1,\"device\":\"radio0\",\"network\":\"lan\",\"mode\":\"ap\",\"isolate\":\"0\",\"user_enable\":\"1\",\"hotspotid\":\"hotspot1\",\"ssid\":\"HOOS\",\"key\":\"development\",\"ttl_increase\":\"0\",\"encryption\":\"psk-mixed\"}}}]},\"statusCode\":200,\"responseUrl\":\"http://192.168.200.2/ubus\",\"headers\":{\"connection\":\"close\",\"transfer-encoding\":\"chunked\",\"x-frame-options\":\"SAMEORIGIN\",\"content-type\":\"application/json\",\"x-node-red-request-node\":\"3156b968\"}}","payloadType":"json","repeat":"","crontab":"","once":false,"onceDelay":0.1,"x":250,"y":300,"wires":[["e1127e10.612fb"]]},{"id":"c119f43.dd8c308","type":"debug","z":"b341cefe.91247","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","x":610,"y":300,"wires":[]},{"id":"e1127e10.612fb","type":"change","z":"b341cefe.91247","name":"","rules":[{"t":"set","p":"result","pt":"msg","to":"**.ssid","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":440,"y":300,"wires":[["c119f43.dd8c308"]]}]

r-01


#4

Thank you very much @Andrei, it's working!

Object%20name