With JSON.stringify, tabs vs spaces is only a choice of indent character: pass a number such as 2 and each level is indented with that many spaces, pass the string "\t" and each level gets one tab. The parsed data is identical either way. What changes is the size of the text and how it looks in an editor, a diff or a log.
How the space argument of JSON.stringify works
The third argument of JSON.stringify decides the indent, and its type matters. According to the JavaScript reference, a number sets how many space characters each level gets, clamped to 10, and values less than 1 mean no space at all. A string is inserted before every nested level instead, so "\t" gives one tab per level.
const user = { name: "Ada", roles: ["admin", "editor"] };
JSON.stringify(user); // compact, no whitespace
JSON.stringify(user, null, 2); // two spaces per level
JSON.stringify(user, null, "\t"); // one tab per level
If you pass null or leave the argument out, no whitespace is added. The second argument is the replacer; pass null when you only want indentation.
Are tabs and spaces both valid JSON?
Yes, both indent styles produce valid JSON. The JSON standard, RFC 8259, allows insignificant whitespace before or after the structural characters (brackets, braces, colon and comma). Its whitespace rule lists exactly four characters: space, horizontal tab, line feed and carriage return.
I checked this by stringifying an object with "\t", parsing it back and comparing it with the original. The data was identical.
Size: how much each indent adds
Tab indentation is the smallest pretty-printed form, because each level costs one character instead of two or four. I ran one small object (a name, a two-item array and a nested address) through Node.js with different space values:
| Third argument | Output length | Notes |
|---|---|---|
| none | 80 characters | one line, no whitespace |
"\t" |
108 characters | one tab per level |
2 |
121 characters | the most common choice |
4 |
147 characters | wide, easy to scan |
12 |
225 characters | same as 10, the cap |
The gap grows with nesting, since every level repeats the indent on every line. For a config file it hardly matters. For a payload you send thousands of times, drop the indent entirely. Compact output beats tabs by more than tabs beat 2 spaces.
The 10-character limit and custom strings
JSON.stringify caps indentation at 10 per level, whether you pass a number or a string. The reference says numbers are clamped to 10 and strings are truncated to their first 10 characters. In my test, 12 gave exactly the same 225-character output as 10 would.
Custom strings are where it breaks. You can pass something like "--" to make the levels visible:
JSON.stringify(user, null, "--");
// {
// --"name": "Ada",
// --"roles": [ ...
That output is no longer JSON. Running it back through JSON.parse fails with Expected property name or '}' in JSON at position 2, because - is not one of the four whitespace characters. Keep custom strings for debug printing only, never for files or API responses.
Which indent should you pick?
Pick the indent your project already uses, and default to 2 spaces when there is no rule. The most common formatter in JavaScript projects, Prettier, ships with useTabs set to false and a tabWidth of 2, so most repos already expect two spaces. Some projects go the other way: one large open-source CMS keeps its core JSON files in tabs and points to an accessibility benefit, since each reader can set how wide a tab looks.
| Situation | Use | Why |
|---|---|---|
| API response or request body | no third argument | smallest output, nobody reads it raw |
| Config file in a JS repo | 2 |
matches the default formatter settings |
| Repo that standardises on tabs | "\t" |
matches the house style, adjustable width |
| Logs you scan by eye | 2 or compact |
compact keeps one record per line |
| Debug output in the console | any | the text is thrown away anyway |
My take: write JSON files with whatever your formatter would produce, so the file does not show up as a whole-file change in the next diff. If you write "\t" from a script and a teammate's editor reformats it to spaces, every line changes and the real edit gets lost. An EditorConfig file with indent_style set to tab or space keeps editors in line with your script.
The same choice in Python
Python's json.dumps works the same way: an integer gives spaces and a string such as "\t" gives tabs. The Python json documentation says string values have been accepted since version 3.2.
import json
json.dumps(data, indent=2) # two spaces
json.dumps(data, indent="\t") # tabs
One difference: Python's default (indent=None) still puts a space after each comma and colon, while JavaScript's compact form has none. There is more on this in the guide to pretty-printing JSON in Python.
Common mistakes with JSON indentation
Most indent problems come from mixing tools, not from JSON.stringify itself.
- Passing the indent as the second argument.
JSON.stringify(obj, 2)treats2as the replacer and ignores it, so you get compact output. UseJSON.stringify(obj, null, 2). - Copying JSON into YAML. JSON accepts tabs, but YAML bans them in indentation, so tab-indented text breaks there. See why YAML fails on tabs.
- The indent does not survive a round trip.
JSON.parsethrows all whitespace away, so stringify again with the indent you want. - Escaped output. If you see
\nand\tas literal text, the value was stringified twice. The post on JSON.stringify vs JSON.parse covers why.
The limitation: indentation is a display choice, and no indent width makes a big document fast to read. For deep or large files, use a collapsible tree view instead. The OnlineViewer JSON formatter re-indents pasted JSON with JSON.stringify and 2 spaces by default, in the browser. For console work, see how to format JSON in the browser console.
FAQ
How do I make JSON.stringify use tabs?
Pass the tab character as the third argument: JSON.stringify(value, null, "\t"). Each nesting level then starts with one more tab.
Does JSON.stringify indentation change the data?
No. Spaces, tabs and line breaks between tokens are insignificant whitespace in JSON, so JSON.parse returns the same value whichever indent you used.
What is the maximum indent in JSON.stringify?
Ten. A number above 10 is treated as 10, and a string longer than 10 characters is cut to its first 10 characters.
Should I indent JSON that I send over an API?
Usually not. Leaving out the third argument gives compact output with no extra whitespace, which is smaller on the wire. Indent only when a person will read the result.