What is the JSON indentation level convention?

JavascriptJson

Javascript Problem Overview


Is there any such thing as the "standard" convention for JSON indentation level? Should it be 2 spaces, 3 spaces, 4 spaces, tabs delimited, or something else?

I tried to come across the official JSON site, but it is not stated there.

Javascript Solutions


Solution 1 - Javascript

JSON is a serialization format, not a presentation format.

As such, there is no "standard" indentation - JSON is typically sent as compactly as possible.

(That said, there is an option to JSON.stringify() to request "pretty printed" JSON - look at the space parameter at the MDN documentation)

Solution 2 - Javascript

There is no standard. The JSON specification permits any number of whitespaces.

However, when you are pretty-printing JSON to make it readable (e.g. in config files) it is good practise to be consistent with the coding conventions of your project and use the same indendation level as you would for an JS object literal - which is often 4 (Crockford) or 2 spaces (Node.js).

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionGlenn MohammadView Question on Stackoverflow
Solution 1 - JavascriptAlnitakView Answer on Stackoverflow
Solution 2 - JavascriptBergiView Answer on Stackoverflow