Saving variables from a JSON object

Hello, I am working on finding a way to do something in an easy way and also run in less amount of time.
I have a function that return a msg.payload in JSON format. It sends totally different messages but they are unique and only repeats if there is new value coming for example

  1. {Voltage: 23, Current:2}
    2.{DEV: 23}

I have a lot of variables coming from one node, around 300 variables. I need to save each value to variable and keep updating it once there is a new value coming out of that node, as the node is overwriting new data each second to the msg.payload

If your data has a different topic for each type (or you can add a topic) then you can store the whole payload in flow or global context using a function node e.g..

var sensors = flow.get("sensors") || {};   //get the sensors state from flow context
var sensorName = msg.topic;                //get sensor name from incoming msg
sensors[sensorName] = msg.payload;         //store value under a key in the sensors object 
flow.set("sensors", sensors);              //update the flow context

Hi @mafify Welcome. You can use the change node to save each value in a different flow variable. for example!!

For basic example:

Captura de pantalla 2021-07-19 a la(s) 1.25.26 p. m.

Captura de pantalla 2021-07-19 a la(s) 1.23.30 p. m.

Captura de pantalla 2021-07-19 a la(s) 1.26.07 p. m.

and you could see in the context:
Captura de pantalla 2021-07-19 a la(s) 1.26.26 p. m.

i hope it helps you!!

That what I did in a function node, but when the second JSON message arrive and it have different topics as ID and MID. The output of change node will output undefined.

Who is that a reply to?

Did you try the function I posted?

Does your data have different topic for different payloads?

1 Like

Hello, yes I just tried it. after the JSON sent first object which is {ID:208}, the function saved it, when second message is sent from JSON which is {L:60}, the function returned it undefined

that's how I tried the function, thank you :sweat_smile:

var sensors = flow.get ("Sensors") || {};
var ID = msg.payload.ID;
sensors[ID] = msg.payload;
return msg; 

Do your messages have individual topics like I asked?

No, messages are only one part, there is no different topics

A function node containing this will merge all previous values with the current values in the payload, if that is what you are looking for

let values = flow.get("values") || {}
Object.assign( values, msg.payload)
flow.set("values", values)
msg.payload = values
return msg;

Example flow:

[{"id":"fc5427b9.f8d0f8","type":"debug","z":"84405ff5.25fa6","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":630,"y":1260,"wires":[]},{"id":"fe1a639c699fc2ec","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"a\":1,\"b\":2}","payloadType":"json","x":210,"y":1240,"wires":[["50c9c922cf4da5e7"]]},{"id":"40f54be8c9bda68c","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"c\": 3}","payloadType":"json","x":190,"y":1300,"wires":[["50c9c922cf4da5e7"]]},{"id":"50c9c922cf4da5e7","type":"function","z":"84405ff5.25fa6","name":"Merge","func":"let values = flow.get(\"values\") || {}\nObject.assign( values, msg.payload)\nflow.set(\"values\", values)\nmsg.payload = values\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":410,"y":1260,"wires":[["fc5427b9.f8d0f8"]]},{"id":"51904c66d8f80f90","type":"inject","z":"84405ff5.25fa6","name":"","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"c\": 4}","payloadType":"json","x":190,"y":1360,"wires":[["50c9c922cf4da5e7"]]}]

By the way, it isn't a JSON object, JSON is always a string. It is a javascript object.

1 Like

That is what I am looking for, Thank you do much!!

The key was to Google for
javascript merge objects

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