Pretty JSON
I first learned about the JSON.stringify()
method from a senior developer during an internship. Since then, I’ve used it A LOT to debug JS code, but one thing that always annoyed me was how it prints the whole JSON object in one long line.
I spent a good 30 minutes or so looking at different Stack Overflow solutions. Through combinations of googling bad keywords and lazy skimming, I found a good great solution.
The answer is in the documentation. Duh, why didn’t I think of that in the first place?
The native JSON.stringify()
method has a built-in option to format readable output. Here’s the syntax:
JSON.stringify(value[, replacer[, space]])
The first parameter value
is the value you want to convert to a JSON string. The second parameter replacer
is basically a filtering function you can pass in to ignore certain values. The third parameter space
is the golden ticket. It is a String or Number object that is used to insert whitespace into the output.
Example:
var myObj = {"foo": "bar", "nums": [{"one": 1, "uno": 1}, {"two": 2}]};
JSON.stringify(myObj, null, 2);
Output:
{
"foo": "bar",
"nums": [
{
"one": 1,
"uno": 1
},
{
"two": 2
}
]
}
Aha!