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.