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 call JSON.stringify with the 3rd argument set to the number of spaces to indent.

For instance, we write

JSON.stringify(object, null, 2)

to call JSON.stringify with the object to convert to a JSON string and 2 to indent each level of the JSON string to 2.

Conclusion

To write formatted JSON in Node.js, we can call JSON.stringify with the 3rd argument set to the number of spaces to indent.

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 *