How to change object "msg.payload" to an array?

hi,

I am trying to work with a HTTP json response to iterate trough all items of the response and I am failing in getting an iterateable array out of the response.

After the HTTP request node I have the json node with "Always convert to JavaScript Object" .

Now I would like to form a new GET request to another server with ...&keyname1=value1&keyname2=value2...
a function where I tries to interate trough the javascript object fails with ""TypeError: msg.payload is not iterable"
in the function I am trying with:
for (var element of msg.payload) {
node.warn(element)
}

I also tried with "msg.payload.length" but as it is a object there is no length :frowning:

so, how can I convert the object to an array?

Thanks! :slight_smile:

[{"id":"b4929bc9.9abd38","type":"inject","z":"6d7b6ac1.9d58e4","name":"Heliotherm MP Values Read","topic":"","payload":"","payloadType":"date","repeat":"45","crontab":"","once":false,"onceDelay":0.1,"x":160,"y":280,"wires":[["a95957af.582608"]]},{"id":"a95957af.582608","type":"http request","z":"6d7b6ac1.9d58e4","name":"api/v1/fastquery/","method":"GET","ret":"obj","paytoqs":false,"url":"http://192.168.0.250:8800/api/v1/fastquery/","tls":"","persist":false,"proxy":"","authType":"","x":380,"y":280,"wires":[["3da0187a.bcd238","a3375ca2.4215b"]]},{"id":"a3375ca2.4215b","type":"json","z":"6d7b6ac1.9d58e4","name":"","property":"payload","action":"obj","pretty":false,"x":660,"y":220,"wires":[["471e78af.991fb8","9d20de65.45e46"]]},{"id":"9d20de65.45e46","type":"function","z":"6d7b6ac1.9d58e4","name":"iterate","func":"\nfor (var element of msg.payload) {\n    node.warn(element)\n}\n\nreturn msg;","outputs":1,"noerr":0,"x":870,"y":260,"wires":[["63f98f12.99146"]]},{"id":"63f98f12.99146","type":"debug","z":"6d7b6ac1.9d58e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1020,"y":260,"wires":[]},{"id":"471e78af.991fb8","type":"debug","z":"6d7b6ac1.9d58e4","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","x":1010,"y":200,"wires":[]}]

can you show us a debug or cut/paste of the type of output you get from the request node ?


here it is.

Thanks!

Hi @Cyber

this post explains how to iterate over the properties of an object - https://stackoverflow.com/questions/684672/how-do-i-loop-through-or-enumerate-a-javascript-object

thank you - works like a charm now. :slight_smile: :grinning:

1 Like

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