Pretty print JSON with native Javascript
The native JSON.stringify has an option to make the output more readable by adding spaces to the string.
Example:
var someObject = { "test": 123, "others": [ { "abc": 1 }, { "xyz": 999 } ] };
JSON.stringify(someObject, undefined, 2);
The return will be:
{
"test": 123,
"others": [
{
"abc": 1
},
{
"xyz": 999
}
]
}
The second parameter is a replacer, which basically works like a filter to ignore certain values.
For more info: https://developer.mozilla.org/en-US/docs/Using_native_JSON
Written by Felipe Elias Philipp
Related protips
1 Response
This is awesome!!.. and extremely simple. There are other alternatives but using other libs and stuff.. this is the best :D Thanks
over 1 year ago
·
Have a fresh tip? Share with Coderwall community!
Post
Post a tip
Did you know?
JavaScript on a page runs with the privileges of that origin. One altered library, tag manager, or snippet can read inputs, change what people see, and call your APIs as the user.
Security insights from Cside
Sponsored by #native_company# — Learn More
#native_title#
#native_desc#