Load multi dim array variable content from file

I have a flow variable (flow.set('settings-array',[6][6])) that I save to a json file. Here is a minimal flow to demonstrate:

[{"id":"5b2e4968d88ade34","type":"file","z":"b5cfb072d3d83deb","name":"","filename":"./settings-array.json","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":670,"y":440,"wires":[[]]},{"id":"cca1a262e2786e7d","type":"json","z":"b5cfb072d3d83deb","name":"","property":"payload","action":"","pretty":false,"x":490,"y":440,"wires":[["5b2e4968d88ade34"]]},{"id":"96feb5edcc2a0ccf","type":"function","z":"b5cfb072d3d83deb","name":"","func":"node.send({payload:flow.get('settings-array')});\n","outputs":1,"timeout":0,"noerr":0,"initialize":"","finalize":"","libs":[],"x":320,"y":480,"wires":[["cca1a262e2786e7d","219d7d10a48c6c88"]]},{"id":"6cf7827d48dd953b","type":"inject","z":"b5cfb072d3d83deb","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":160,"y":480,"wires":[["96feb5edcc2a0ccf"]]},{"id":"219d7d10a48c6c88","type":"debug","z":"b5cfb072d3d83deb","name":"debug","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"true","targetType":"full","statusVal":"","statusType":"auto","x":490,"y":500,"wires":[]}]

This works well and upon examining the file content, I have all the data there.

But I cannot figure out how to load it back in from the file to the variable. Any suggestions please?

If you save the data as JSON you can simply pass it through a JSON node to recreate it.

Array -> JSON node -> write file

Read file -> JSON node -> array

Alternatively, you could just use persistent context?

Read file -> JSON node -> array

I have been trying but no luck yet (only been using node-red for a few days so still learning)

persistent context

Something to read up on. Thank you.

But I am saving the values to share them on another computer.

Here is a demo
chrome_mzHGI4GD1j

[{"id":"5b2e4968d88ade34","type":"file","z":"99e019d5.6f0748","name":"","filename":"./settings-array.json","filenameType":"str","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":930,"y":240,"wires":[[]]},{"id":"6cf7827d48dd953b","type":"inject","z":"99e019d5.6f0748","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":400,"y":240,"wires":[["6b898c5a6fc321c0"]]},{"id":"3482428ce7520a99","type":"inject","z":"99e019d5.6f0748","name":"dummy data [[1,2,3],[4,5,6]]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[1,2,3],[4,5,6]]","payloadType":"json","x":450,"y":120,"wires":[["eeb467cd1da1d9b6"]]},{"id":"eeb467cd1da1d9b6","type":"change","z":"99e019d5.6f0748","name":"","rules":[{"t":"set","p":"settings-array","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":700,"y":140,"wires":[["d45a25ffbb5882d5"]]},{"id":"e9ae184d298ba242","type":"inject","z":"99e019d5.6f0748","name":"dummy data [[3,3,3],[6,6,6]]","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"[[3,3,3],[6,6,6]]","payloadType":"json","x":450,"y":160,"wires":[["eeb467cd1da1d9b6"]]},{"id":"6b898c5a6fc321c0","type":"change","z":"99e019d5.6f0748","name":"get flow.settings-array","rules":[{"t":"set","p":"payload","pt":"msg","to":"settings-array","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":240,"wires":[["5b2e4968d88ade34"]]},{"id":"7684ecc471f8bb5e","type":"inject","z":"99e019d5.6f0748","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":400,"y":300,"wires":[["d5022c077495716b"]]},{"id":"d5022c077495716b","type":"file in","z":"99e019d5.6f0748","name":"","filename":"./settings-array.json","filenameType":"str","format":"utf8","chunk":false,"sendError":false,"encoding":"none","allProps":false,"x":590,"y":300,"wires":[["ab92e2c699ea8138"]]},{"id":"ab92e2c699ea8138","type":"json","z":"99e019d5.6f0748","name":"","property":"payload","action":"","pretty":false,"x":750,"y":300,"wires":[["2e1fdaf7cda04859"]]},{"id":"2e1fdaf7cda04859","type":"change","z":"99e019d5.6f0748","name":"","rules":[{"t":"set","p":"settings-array","pt":"flow","to":"payload","tot":"msg"}],"action":"","property":"","from":"","to":"","reg":false,"x":940,"y":300,"wires":[[]]},{"id":"d45a25ffbb5882d5","type":"debug","z":"99e019d5.6f0748","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":910,"y":140,"wires":[]},{"id":"a751542558043510","type":"debug","z":"99e019d5.6f0748","name":"","active":true,"tosidebar":true,"console":false,"tostatus":true,"complete":"payload","targetType":"msg","statusVal":"payload","statusType":"auto","x":910,"y":380,"wires":[]},{"id":"d60b14d75da86f01","type":"inject","z":"99e019d5.6f0748","name":"","props":[{"p":"payload"},{"p":"topic","vt":"str"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"","payloadType":"date","x":400,"y":380,"wires":[["bd7c9f17b6b9da59"]]},{"id":"bd7c9f17b6b9da59","type":"change","z":"99e019d5.6f0748","name":"get flow.settings-array","rules":[{"t":"set","p":"payload","pt":"msg","to":"settings-array","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":640,"y":380,"wires":[["a751542558043510"]]}]
1 Like

Wow, @Steve-Mcl, I cannot believe that you took the effort to create that, thank you very much indeed, it filled a gap in my knowledge and I am on the correct path now.

1 Like

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