Trouble with working through the Node Red tutorials

I have been working through the Node Red tutorials and got to the point of building arrays with the Star Wars data from the site, “people”: “https://swapi.info/api/people”. I have then followed the tutorial to convert a text label “height” value from “172” to a numerical value of 172.

The problem comes from referencing the correct part of the array. I seem only to be able to reference on record [0] of the array which then only changes the height value of one record. When I try to move up a level it gives me the error message - “Invalid JSONata expression: Argument 1 of function “number” does not match function signature”.

Hi & welcome to the forum.

I dont know which tutorial you are refering to but for arrays of data, you need to look at the split and join nodes.

You pass the msg (the one with the payload array) to a split node - that then splits the payload into x number of sequential messages - each containing the 1 element of the payload array. You can then operate on msg.payload.height as required. Then you pass them back into a join node to re-create the original array.

http-request → split change → join → debug

See the build in exampes (CTRL-I --> Examples)


My advice - avoid JSONata expressions until you get up-to-speed with Node-RED (J expressions are much less commonly used and only a handful of crazy folk know more than the basics :wink: )

Hi Steve, Thanks for your help. The tutorial is actually on Loops as shown on the screen shot below:-

As you can see below I have been able to write the code to change the array value [0] for the records "height" and "mass" to numerical values but my code only works on record (or line) 0 rather than filing through the whole array. When I take the [0] reference out of the code it gives me the error message i stated above.

Example of applying a function to all height properties in an array, using JSONata transform operator. Other Operators · JSONata
e.g

[{"id":"960e96e5343522c2","type":"inject","z":"613df62afc8a16bf","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[{\"height\":\"123\",\"other\":\"any\"},{\"height\":\"456\",\"other\":\"more\"}]","payloadType":"json","x":155,"y":720,"wires":[["3318ae36c068cdd2"]]},{"id":"3318ae36c068cdd2","type":"change","z":"613df62afc8a16bf","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"$$.payload ~> |$|{\"height\": $number($.height)}|","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":340,"y":720,"wires":[["8abc35ac1c8fcbdc"]]},{"id":"8abc35ac1c8fcbdc","type":"debug","z":"613df62afc8a16bf","name":"debug 18","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":525,"y":720,"wires":[]}]

But if you are splitting the array you can use your OG example on each object after the split no alos.

If i remember correctly, in a jsonata expression you refer to payload rather than msg.payload

That said, I agree with @Steve-Mcl regarding Jsonata (ie the J: expression in your change node)
The learning curve for javascript in a function node is gentler than that for Jsonata.

You can use msg as JSONata will go into compatibility mode. But it is best to avoid using msg and use $$ instead, or omit $$ but this may bite you later in map loops and more complicate JSONata expressions.