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.