Categories
JavaScript Answers

How to write formatted JSON in Node.js?

Spread the love

Sometimes, we want to write formatted JSON in Node.js.

In this article, we’ll look at how to write formatted JSON in Node.js.

How to write formatted JSON in Node.js?

To write formatted JSON in Node.js, we can use the JSON.stringify method.

For instance, we write

const output = JSON.parse(json);
const printToFile = JSON.stringify(output, null, "\t");

to call JSON.parse to parse the json JSON string into an object.

Then we call JSON.stringigfy with output and '\t' to return a JSON string that has indentation with tabs.

Conclusion

To write formatted JSON in Node.js, we can use the JSON.stringify method.

Leave a Reply

Your email address will not be published.

If you like the content of this blog, subscribe to my email list to get exclusive articles not available to anyone else.