Categories
JavaScript Answers

How to write formatted JSON in Node.js?

Spread the love

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.

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 *