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.