To write formatted JSON in Node.js, we call JSON.stringify
.
For instance, we write
const formatted = JSON.stringify(object, null, 4);
to call JSON.stringify
to return a JSON stringify with the object
object converted to a JSON string.
We call it with 4 as the 3rd argument to indent each level with 4 spaces.