# Help with writing files

**URL:** <https://discourse.nodered.org/t/help-with-writing-files/85314>\
**Category:** General\
**Created:** [8 February 2024 08:27 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314 "2024-02-08T08:27:37Z")\
**Posts on this page:** 10\
**Page:** 1

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 February 2024 08:27 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/1 "2024-02-08T08:27:37Z")

</div>

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

This is it:

```auto
{"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:

```auto
{
"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.

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 February 2024 08:37 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/2 "2024-02-08T08:37:53Z")

</div>

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

 ![chrome_screenshot_8 Feb 2024 08_42_12 GMT](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/e/6/e6c7c92e86b19a6ac734578d8c1c10539db7d69d.png)

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 February 2024 08:40 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/3 "2024-02-08T08:40:06Z")

</div>

☹😢  
Sorry, _that does not compute_.......

Could you do a screen shot of what you mean?

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 February 2024 08:56 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/4 "2024-02-08T08:56:39Z")

</div>

Something like this?

```auto
[{"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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 February 2024 09:06 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/5 "2024-02-08T09:06:37Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 February 2024 09:19 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/6 "2024-02-08T09:19:28Z")

</div>

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.

```auto
{
    "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
    }
}

```

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 February 2024 09:22 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/7 "2024-02-08T09:22:04Z")

</div>

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

Ah!

Ok, thanks.

Handy to know.

Appreciated.

---

<div class="post-metadata">

**Author:** ![E1cid](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/e1cid/32/77971_2.png) [@E1cid](https://discourse.nodered.org/u/E1cid)\
**Post date:** [8 February 2024 09:26 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/8 "2024-02-08T09:26:05Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![Trying\_to\_learn](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/trying_to_learn/32/28400_2.png) [@Trying\_to\_learn](https://discourse.nodered.org/u/Trying_to_learn)\
**Post date:** [8 February 2024 09:27 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/9 "2024-02-08T09:27:44Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [22 February 2024 09:28 UTC](https://discourse.nodered.org/t/help-with-writing-files/85314/10 "2024-02-22T09:28:10Z")

</div>

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