Your Web News in One Place

Help Webnuz

Referal links:

Sign up for GreenGeeks web hosting
May 25, 2021 03:08 pm GMT

Pretty printing JSON.stringify

Most of use JSON.stringify a lot to avoid the infamous "[object Object]". But did you know that it had a few more arguments?

JSON.stringify takes a total of 3 arguments. The first one is the data, the second is a replacer function, and the third one is the indentation.

The main topic of this article is the third argument. If you provide a string as the third argument, that string will be used as indentation. Here's an example:

JSON.stringify({a: 'B', c: {d: 'e'}})// => {"a":"B","c":{"d":"e"}}JSON.stringify({a: 'B', c: {d: 'e'}}, null, "  ")// => // {//   "a": "B",//   "c": {//     "d": "e"//   }// }JSON.stringify({a: 'B', c: {d: 'e'}}, null, "test")// =>// {// test"a": "B",// test"c": {// testtest"d": "e"// test}// }

You can also pass in a number instead. If you do so, that many spaces will be inserted as indentation:

JSON.stringify({a: 'B', c: {d: 'e'}}, null, 2)// => // {//   "a": "B",//   "c": {//     "d": "e"//   }// }

Hope this helps you while debugging sometime!


Original Link: https://dev.to/siddharthshyniben/pretty-printing-json-stringify-4pd3

Share this article:    Share on Facebook
View Full Article

Dev To

An online community for sharing and discovering great ideas, having debates, and making friends

More About this Source Visit Dev To