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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *