# Format JSON before writing to a file

**URL:** <https://discourse.nodered.org/t/format-json-before-writing-to-a-file/62838>\
**Category:** General\
**Created:** [19 May 2022 09:24 UTC](https://discourse.nodered.org/t/format-json-before-writing-to-a-file/62838 "2022-05-19T09:24:59Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [19 May 2022 09:24 UTC](https://discourse.nodered.org/t/format-json-before-writing-to-a-file/62838/1 "2022-05-19T09:24:59Z")

</div>

Dear Father, I am sinning - I'm writing a flow to modify flows.json ☹

Now that's over with 🙂 is there an easy method of nicely formatting my JSON before writing it to file

e.g instead of this

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/0/8/08d413261aae5942f48572e02d9c704329e86a82.png)

I get this  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/a/1/a17345f7d694a749ef0a20699d0bf43638a6206f.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 May 2022 09:29 UTC](https://discourse.nodered.org/t/format-json-before-writing-to-a-file/62838/2 "2022-05-19T09:29:35Z")

</div>

Via function node...

```auto
JSON.stringify(object, null, 4); //4 is the number of spaces for formatting

```

If your object is JSON, then run `JSON.parse` first

```auto
JSON.stringify(JSON.parse(flowString), null, 2);

```

---

<div class="post-metadata">

**Author:** ![cymplecy](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/cymplecy/32/2773_2.png) [@cymplecy](https://discourse.nodered.org/u/cymplecy)\
**Post date:** [19 May 2022 09:32 UTC](https://discourse.nodered.org/t/format-json-before-writing-to-a-file/62838/3 "2022-05-19T09:32:15Z")

</div>

Guess what I just spotted 🙂  
 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/f/0/f05ecd4587b37f4302c76b1f7b6920738ef4401b.png)

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [19 May 2022 09:33 UTC](https://discourse.nodered.org/t/format-json-before-writing-to-a-file/62838/4 "2022-05-19T09:33:37Z")

</div>

Ha - never noticed that!

---

<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:** [18 July 2022 09:34 UTC](https://discourse.nodered.org/t/format-json-before-writing-to-a-file/62838/5 "2022-07-18T09:34:34Z")

</div>

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