Creating global variables from JSON object

Hello,

I'm trying to create a bit of a system to store configuration data for my project, but allow it to be edited through the node red dashbord interface. I think the best option to edit it is a form with a multi-line text box where I can load in the config JSON and then alter, and save it again.
From there I'd like to then use the JSON keys to set the name of some global variables using the keys names as variable names.

So far I've been able to make something that will let me load, edit, and store the JSON configuration, but I cannot figure out how to parse the JSON into global variables in a function node. How would I do that?

[{"id":"a70a5e2c.20009","type":"file in","z":"119d8187.00afee","name":"Load config/transportPositioning.cfg","filename":"/home/pi/config/transportPositioning.cfg","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":700,"y":840,"wires":[["a820c818.adb0e8"]]},{"id":"a820c818.adb0e8","type":"json","z":"119d8187.00afee","name":"","property":"payload","action":"","pretty":true,"x":930,"y":840,"wires":[["29af4a86.1e3a46","1bac61e.1ef459e","953e783f.0b0c68"]]},{"id":"1bac61e.1ef459e","type":"ui_form","z":"119d8187.00afee","name":"","label":"","group":"9aea8836.79fcc8","order":18,"width":0,"height":0,"options":[{"label":"transportPositioning.cfg","value":"transportPos","type":"multiline","required":true,"rows":15}],"formValue":{"transportPos":""},"payload":"","submit":"submit","cancel":"","topic":"","x":610,"y":920,"wires":[["8665d718.234b58"]]},{"id":"c0bcf4c.171f308","type":"inject","z":"119d8187.00afee","name":"load","props":[{"p":"payload"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"1","payloadType":"str","x":290,"y":840,"wires":[["6e8837fc.f6ef28"]]},{"id":"8665d718.234b58","type":"file","z":"119d8187.00afee","name":"Write config/transportPositioning.cfg","filename":"/home/pi/config/transportPositioning.cfg","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":870,"y":920,"wires":[["953e783f.0b0c68"]]},{"id":"953e783f.0b0c68","type":"function","z":"119d8187.00afee","name":"Create/Update variables","func":"\n\nvar myObj = msg.payload;\nfor (var x in myObj) {\n    global.set(x,1234);\n}\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1190,"y":920,"wires":[["584bc2e.b8b053c"]]},{"id":"584bc2e.b8b053c","type":"debug","z":"119d8187.00afee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1390,"y":920,"wires":[]},{"id":"6e8837fc.f6ef28","type":"ui_button","z":"119d8187.00afee","name":"Read config","group":"9aea8836.79fcc8","order":17,"width":0,"height":0,"passthru":true,"label":"Read config","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":450,"y":840,"wires":[["a70a5e2c.20009"]]},{"id":"29af4a86.1e3a46","type":"debug","z":"119d8187.00afee","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":840,"wires":[]},{"id":"9aea8836.79fcc8","type":"ui_group","name":"Transport positioning","tab":"3b176924.a72a46","order":3,"disp":true,"width":"6","collapse":false},{"id":"3b176924.a72a46","type":"ui_tab","name":"Operation parameters","icon":"settings","order":3,"disabled":false,"hidden":false}]

If you want to test, paste this into the form text box and press submit to generate the file:

{
"position1":100,
"position2":500,
"position3":640
}

I seem to be able to extract the top level JSON key (transportPos) but anything above that just comes out a mess.

Pass the JSON string through a JSON node (to turn it into a Js object) then use a change node to store it in global or flow context.

I'm going to have about a dozen variables... I don't fancy having a change node for each one of them so I was hoping to use a function node to parse/store.
It also doesn't seem to do much passing it through a JSON node.

Do you mean you're going to have a dozen variables in the one JSON string? If so then you just pass the full JSON string through one JSON node and you have access to all of the properties within the object.

Yes, I want to store all the variables in a file, so I figured the easiest way would be to store it as a JSON string and handle it all at once. My problem (as I explained) is that I don't know how to parse the JSON from the file into global variables.

If you look at the flow I posted and paste in the values as I explained it will make more sense.

And i explained how to do it...

I looked at your flow and it made no sense.

in simple terms...

  1. text box (user enters JSON)
  2. --> msg.payload (with JSON string) --> JSON node (turns it into JS object) in msg.payload
  3. --> change node SET global.myData to value of msg.payload

If you want to save it to file then add step 4

  1. --> file node

What you're suggesting is very similar to what, just configured slightly differently to enable me to populate the text box from the file.

The key thing I want to do is storing all the JSON items as individual global variables based on their labels. This is why I was keen to use a function node (seems easier to parse the data) and requested assistance on that front, since I'm quite unfamiliar with JS.

EDIT: Finally figured it out. Steve, thanks for your help, apologies if I wasted your time by not being clearer!

Just had to figure out the JS aspect of it, here's an updated flow:

[{"id":"3e3181ad.05b0ce","type":"file in","z":"1815768a.d2e249","name":"Load config/transportPositioning.cfg","filename":"/home/pi/config/transportPositioning.cfg","format":"utf8","chunk":false,"sendError":false,"encoding":"none","x":640,"y":600,"wires":[["7c853ada.ce1d04"]]},{"id":"7c853ada.ce1d04","type":"json","z":"1815768a.d2e249","name":"","property":"payload","action":"","pretty":true,"x":870,"y":600,"wires":[["4ea38d7f.75c744","9c7bd1be.021b4","c9f4bc90.b4ef7"]]},{"id":"9c7bd1be.021b4","type":"ui_form","z":"1815768a.d2e249","name":"","label":"","group":"9aea8836.79fcc8","order":18,"width":0,"height":0,"options":[{"label":"transportPositioning.cfg","value":"transportPos","type":"multiline","required":true,"rows":15}],"formValue":{"transportPos":""},"payload":"","submit":"submit","cancel":"","topic":"","x":550,"y":680,"wires":[["43747a3c.1d6ca4"]]},{"id":"43747a3c.1d6ca4","type":"file","z":"1815768a.d2e249","name":"Write config/transportPositioning.cfg","filename":"/home/pi/config/transportPositioning.cfg","appendNewline":false,"createDir":true,"overwriteFile":"true","encoding":"none","x":810,"y":680,"wires":[["c9f4bc90.b4ef7"]]},{"id":"c9f4bc90.b4ef7","type":"function","z":"1815768a.d2e249","name":"Create/Update variables","func":"\n\nvar paramObj = JSON.parse(msg.payload.transportPos);\n\nfor (var x in paramObj) {\n    global.set(x,paramObj[x]);\n}\n\nreturn msg;\n\n\n","outputs":1,"noerr":0,"initialize":"","finalize":"","x":1150,"y":680,"wires":[["ce76a08a.1dd"]]},{"id":"ce76a08a.1dd","type":"debug","z":"1815768a.d2e249","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":1390,"y":680,"wires":[]},{"id":"3fb76071.2b47d","type":"ui_button","z":"1815768a.d2e249","name":"Read config","group":"9aea8836.79fcc8","order":17,"width":0,"height":0,"passthru":true,"label":"Read config","tooltip":"","color":"","bgcolor":"","icon":"","payload":"","payloadType":"str","topic":"","x":390,"y":600,"wires":[["3e3181ad.05b0ce"]]},{"id":"4ea38d7f.75c744","type":"debug","z":"1815768a.d2e249","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"payload","targetType":"msg","statusVal":"","statusType":"auto","x":1110,"y":600,"wires":[]},{"id":"f7f25b3b.bbe368","type":"link in","z":"1815768a.d2e249","name":"On startup","links":["da39128d.b84f4"],"x":220,"y":600,"wires":[["3fb76071.2b47d"]],"l":true},{"id":"9aea8836.79fcc8","type":"ui_group","name":"Transport positioning","tab":"3b176924.a72a46","order":3,"disp":true,"width":"6","collapse":false},{"id":"3b176924.a72a46","type":"ui_tab","name":"Operation parameters","icon":"settings","order":3,"disabled":false,"hidden":false}]```
1 Like

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