Help with writing files

I have a "config" file that slowly is growing.

This is it:

{"1":{"number":0},"2":{"number":1},"3":{"number":1},"4":{"number":0},"5":{"number":1},"6":{"number":0},"7":{"number":0},"8":{"number":0},"9":{"number":1},"10":{"number":1},"11":{"number":0},"12":{"number":0},"13":{"number":1},"14":{"number":0},"15":{"number":1},"16":{"number":0},"17":{"number":0},"18":{"number":0}}

Nothing special.

But a slight pain to edit/add.

Is there a way I could make it more like:

{
"1":{"number":0},
"2":{"number":1},
"3":{"number":1},
"4":{"number":0},
"5":{"number":1},
"6":{"number":0},
"7":{"number":0},
"8":{"number":0},
"9":{"number":1},
"10":{"number":1},
"11":{"number":0},
"12":{"number":0},
"13":{"number":1},
"14":{"number":0},
"15":{"number":1},
"16":{"number":0},
"17":{"number":0},
"18":{"number":0}
}

Just then when I add a new line I can do it a bit easier.
And it makes it a bit more human readable if I need to check the status.

Thanks.

Oh, sorry.

This is how I write the file.

[{"id":"d4ffc695.91eec8","type":"change","z":"bdc47459.27c378","g":"98c1a978c6893072","name":"get flow.active","rules":[{"t":"set","p":"payload","pt":"msg","to":"active","tot":"flow"}],"action":"","property":"","from":"","to":"","reg":false,"x":480,"y":770,"wires":[["a15038ab.34cab"]]},{"id":"a15038ab.34cab","type":"function","z":"bdc47459.27c378","g":"98c1a978c6893072","name":"Set path/name","func":"var path = \"/home/pi/.node-red/public/\";\nmsg.filename = path + \"active.txt\";\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":345,"y":810,"wires":[["807cc452.203388"]],"l":false},{"id":"807cc452.203388","type":"file","z":"bdc47459.27c378","g":"98c1a978c6893072","name":"","filename":"","appendNewline":true,"createDir":false,"overwriteFile":"true","encoding":"none","x":460,"y":810,"wires":[[]]}]

I'm not sure I would need to edit the reading, as I'm guessing the <cr> are taken out when read.

You could add a JSON node, to convert to JSON string, and check format JSON string

1 Like

:frowning: :cry:
Sorry, that does not compute.......

Could you do a screen shot of what you mean?

Something like this?

[{"id":"3477d6354f11e7a1","type":"function","z":"0918ee609bf69fc7","name":"function 37","func":"msg.payload = { \"1\": { \"number\": 0 }, \"2\": { \"number\": 1 }, \"3\": { \"number\": 1 }, \"4\": { \"number\": 0 }, \"5\": { \"number\": 1 }, \"6\": { \"number\": 0 }, \"7\": { \"number\": 0 }, \"8\": { \"number\": 0 }, \"9\": { \"number\": 1 }, \"10\": { \"number\": 1 }, \"11\": { \"number\": 0 }, \"12\": { \"number\": 0 }, \"13\": { \"number\": 1 }, \"14\": { \"number\": 0 }, \"15\": { \"number\": 1 }, \"16\": { \"number\": 0 }, \"17\": { \"number\": 0 }, \"18\": { \"number\": 0 } }\n\nreturn msg;","outputs":1,"noerr":0,"initialize":"","finalize":"","libs":[],"x":2880,"y":1830,"wires":[["9ecede1adda736bc","f6098098eee3438f"]]},{"id":"f6098098eee3438f","type":"json","z":"0918ee609bf69fc7","name":"","property":"payload","action":"","pretty":false,"x":2860,"y":1870,"wires":[["e842c4be14b9e0f5"]]},{"id":"e842c4be14b9e0f5","type":"string","z":"0918ee609bf69fc7","name":"","methods":[{"name":"replaceAll","params":[{"type":"str","value":","},{"type":"str","value":",\\r\\n"}]}],"prop":"payload","propout":"payload","object":"msg","objectout":"msg","x":3010,"y":1870,"wires":[["63c417664a0c5dc5","7059d9aedbddd926"]]},{"id":"7059d9aedbddd926","type":"change","z":"0918ee609bf69fc7","name":"","rules":[{"t":"set","p":"filename","pt":"msg","to":"/home/me/.node-red/public/temp.txt","tot":"str"}],"action":"","property":"","from":"","to":"","reg":false,"x":3180,"y":1900,"wires":[["8b5fbe8c7fa079c4","0f8ad92266f28e12"]]},{"id":"8b5fbe8c7fa079c4","type":"file","z":"0918ee609bf69fc7","name":"","filename":"filename","filenameType":"msg","appendNewline":true,"createDir":true,"overwriteFile":"true","encoding":"none","x":3390,"y":1870,"wires":[["cf9ea8a1e7157dc6"]]}]

Seems to work.

Thanks.

(Sorry, I needed the string node to add the CR/LF between each entry. Unless I missed a trick somewhere.)

I added a screenshot, you must Have missed it.

You would not need the string node if you checked format JSON string, as I mentioned, the output would be.

{
    "1": {
        "number": 0
    },
    "2": {
        "number": 1
    },
    "3": {
        "number": 1
    },
    "4": {
        "number": 0
    },
    "5": {
        "number": 1
    },
    "6": {
        "number": 0
    },
    "7": {
        "number": 0
    },
    "8": {
        "number": 0
    },
    "9": {
        "number": 1
    },
    "10": {
        "number": 1
    },
    "11": {
        "number": 0
    },
    "12": {
        "number": 0
    },
    "13": {
        "number": 1
    },
    "14": {
        "number": 0
    },
    "15": {
        "number": 1
    },
    "16": {
        "number": 0
    },
    "17": {
        "number": 0
    },
    "18": {
        "number": 0
    }
}
1 Like

I saw the screen shot of the flow, not the output.

Ah!

Ok, thanks.

Handy to know.

Appreciated.

You still missed the check format JSON string in screenshot.

I did not have output as i only added nodes to take screenshot for you, and was to lazy to copy data and add it to a inject node, as you should of really done that as a courtesy to those helping.

Yeah, and I thought I said I missed it.

My parsing is not working as good as I would like.

Saw the last bit of the text line and checked the JSON node that is writing the file.

It works with the formatted (extended) output.

Again, I hope I can remember this trick.

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