Indent JSON with JavaScript
Working with XML and JavaScript is a nightmare, which is why JSON has become gold in the development community. Hell, I even wrote a function to turn XML to JSON with JavaScript. If you want to turn an existing object into well formatted JSON, you can you JSON.stringify(obj)
, but you already know that. What you may not know is that you can do pretty formatting when generating JSON from objects!
The secret is using the third JSON.stringify
argument which represents the space indentation levels:
var formatted = JSON.stringify(myObject, null, 2);
/*
Result:
{
"myProp": "myValue",
"subObj": {
"prop": "value"
}
}
*/
The resulting JSON representation will be formatted and indented with two spaces!
My team mate Edna Piranha is not only an awesome hacker; she's also a fantastic philosopher! Communication and online interactions is a subject that has kept her mind busy for a long time, and it has also resulted in a bunch of interesting experimental projects...
Client-side APIs on mobile and desktop devices are quickly providing the same APIs. Of course our mobile devices got access to some of these APIs first, but those APIs are slowly making their way to the desktop. One of those APIs is the getUserMedia API...
As you may know, HTML5 has introduced several new input types: number, date, color, range, etc. The question is: should you
start using these controls or not? As much as I want to say "Yes", I think they are not yet ready for any real life...
The <canvas>
element has been a revelation for the visual experts among our ranks. Canvas provides the means for incredible and efficient animations with the added bonus of no Flash; these developers can flash their awesome JavaScript skills instead. Here are nine unbelievable canvas demos that...
For more advanced formatting, I’ve developed a tool for the intent:
https://github.com/MaxArt2501/json-fmt
It works a client library or a server module for node/io.js, has a CLI, and a Grunt and a Gulp plugin.
Of course, if you need speed just use
JSON.stringify
.Sorry for the self-promotion.
You can also format a JSON file in the terminal with a single command.