JSON means JavaScript Object Notation. This is one of the reasons why pretty-printing is implemented natively in JSON.stringify(). The third argument in it pretty prints and sets the spacing to use −
Example
let a = { name: "A", age: 35, address: { street: "32, Baker Street", city: "Chicago" } } console.log(JSON.stringify(a, null, 4))
Output
{ "name": "A", "age": 35, "address": { "street": "32, Baker Street", "city": "Chicago" } }
Note that we used a JS object here. This works fine for JSON Strings as well, but they first needed to be parse to JS objects using JSON.parse.
Example
let jsonStr = '{"name":"A","age":35,"address":{"street":"32, Baker Street","city":"Chicago"}}' console.log(JSON.stringify(JSON.parse(jsonStr), null, 2))
Output
{ "name": "A", "age": 35, "address": { "street": "32, Baker Street", "city": "Chicago" } }