Splitting an object with variable keys

Hi,

I am a noob and trying to solve the following problem.
I have a node which outputs an object with 2 keys, I would like to extract the values of these keys and save them for further processing.

I know you can directly access the value by copying the path from the debug screen and use this path to get the value belonging to that key.
In my case, the key is actually a timestamp and therefore changing between every message.

What would be the easiest option to get the values while the keys are changing?

This is the object that the function node returns, I would like to capture the values (15389 and 19733) belonging to the two timestamps in from "result:"

{"result":{"2024-08-30":15389,"2024-08-31":19733},"message":{"code":0,"type":"success","text":"","pid":"3elWV60q","info":{"latitude":12.5929,"longitude":8.012,"distance":0.07,"place":"Grotestraat 109, 5321 VB Boxmeer, Netherlands","timezone":"Europe/Amsterdam","time":"2024-08-30T16:28:41+02:00","time_utc":"2024-08-30T14:28:41+00:00"},"ratelimit":{"zone":"IP 91.132.41.166","period":3600,"limit":12,"remaining":5}}}

The split node can help:

[{"id":"a9b68038b3fd6efa","type":"inject","z":"067cd4666c3ef5bc","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":450,"y":200,"wires":[["7c45f0274a214743"]]},{"id":"7c45f0274a214743","type":"function","z":"067cd4666c3ef5bc","name":"your payload","func":"msg.payload = {\"result\":{\"2024-08-30\":15389,\"2024-08-31\":19733},\"message\":{\"code\":0,\"type\":\"success\",\"text\":\"\",\"pid\":\"3elWV60q\",\"info\":{\"latitude\":12.5929,\"longitude\":8.012,\"distance\":0.07,\"place\":\"Grotestraat 109, 5321 VB Boxmeer, Netherlands\",\"timezone\":\"Europe/Amsterdam\",\"time\":\"2024-08-30T16:28:41+02:00\",\"time_utc\":\"2024-08-30T14:28:41+00:00\"},\"ratelimit\":{\"zone\":\"IP 91.132.41.166\",\"period\":3600,\"limit\":12,\"remaining\":5}}}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":680,"y":200,"wires":[["8f93026945909b6e","c81928cb22736e41"]]},{"id":"8f93026945909b6e","type":"debug","z":"067cd4666c3ef5bc","name":"debug 1","active":false,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":950,"y":200,"wires":[]},{"id":"6819429be5001ceb","type":"split","z":"067cd4666c3ef5bc","name":"","splt":"\\n","spltType":"str","arraySplt":1,"arraySpltType":"len","stream":false,"addname":"topic","property":"payload","x":750,"y":280,"wires":[["e814da241eb085b1"]]},{"id":"e814da241eb085b1","type":"debug","z":"067cd4666c3ef5bc","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":900,"y":280,"wires":[]},{"id":"c81928cb22736e41","type":"change","z":"067cd4666c3ef5bc","name":"","rules":[{"t":"set","p":"payload","pt":"msg","to":"payload.result","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":590,"y":280,"wires":[["6819429be5001ceb"]]}]

You can also use JSONata in a change node to convert payload.result to an array
e.g.

[{"id":"a9b68038b3fd6efa","type":"inject","z":"d1395164b4eec73e","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":260,"y":80,"wires":[["7c45f0274a214743"]]},{"id":"7c45f0274a214743","type":"function","z":"d1395164b4eec73e","name":"your payload","func":"msg.payload = {\"result\":{\"2024-08-30\":15389,\"2024-08-31\":19733},\"message\":{\"code\":0,\"type\":\"success\",\"text\":\"\",\"pid\":\"3elWV60q\",\"info\":{\"latitude\":12.5929,\"longitude\":8.012,\"distance\":0.07,\"place\":\"Grotestraat 109, 5321 VB Boxmeer, Netherlands\",\"timezone\":\"Europe/Amsterdam\",\"time\":\"2024-08-30T16:28:41+02:00\",\"time_utc\":\"2024-08-30T14:28:41+00:00\"},\"ratelimit\":{\"zone\":\"IP 91.132.41.166\",\"period\":3600,\"limit\":12,\"remaining\":5}}}\nreturn msg;","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":490,"y":80,"wires":[["c81928cb22736e41"]]},{"id":"c81928cb22736e41","type":"change","z":"d1395164b4eec73e","name":"","rules":[{"t":"set","p":"payload.result","pt":"msg","to":"$$.payload.result.*","tot":"jsonata"}],"action":"","property":"","from":"","to":"","reg":false,"x":420,"y":160,"wires":[["e814da241eb085b1"]]},{"id":"e814da241eb085b1","type":"debug","z":"d1395164b4eec73e","name":"debug 2","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":710,"y":160,"wires":[]}]

Or similar in a function node using Javascript

This is exactly what I was looking for, this is working great !

I had tried a similar setup, but got a slightly different result :slight_smile:
Although this extracts the values nicely, it is a bit harder for me to assign them to variables for further processing since it results in 2 messages coming after each other.
The other suggestion that puts the results in an array is easier for me in this particular usecase.

Thanks for thinking with me !

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