An Interest In:
Web News this Week
- April 24, 2024
- April 23, 2024
- April 22, 2024
- April 21, 2024
- April 20, 2024
- April 19, 2024
- April 18, 2024
May 25, 2021 03:08 pm GMT
Original Link: https://dev.to/siddharthshyniben/pretty-printing-json-stringify-4pd3
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:
Tweet
View Full Article
Dev To
An online community for sharing and discovering great ideas, having debates, and making friendsMore About this Source Visit Dev To