Skip to content
TextBench Lab

Developer

Format or Minify JSON? When to Use Each

Published 5 min read

On this page
  1. Which one should you use, and when?
  2. What does formatting actually do to JSON?
  3. Does minifying change the data?
  4. How many bytes does minifying save?
  5. Should you indent with 2 spaces, 4 spaces or a tab?
  6. Can a formatter change your values?
  7. When is sorting keys useful?
  8. Format and minify: quick questions

Format JSON when a person needs to read it, and minify it when a program sends or stores it. Formatting (also called pretty printing or beautifying) adds line breaks and indentation so the structure is visible; minifying removes all whitespace outside strings so the file is as small as possible. Neither changes the data, so you can switch between them at any time.

This guide uses one real record to show what each option does, how much space minifying saves, and why a good formatter never touches your values.

Which one should you use, and when?

SituationChooseWhy
Reading an API responseFormatYou can see the nesting at a glance
Reviewing a config changeFormatLine-by-line diffs stay readable
Sending data over a networkMinifyFewer bytes to transfer
Storing many recordsMinifySmaller files
Comparing two filesFormat + sort keysSame data lines up the same way

If you are unsure, format. Readability helps more often than a few saved bytes, and a program can minify at the moment it sends the data.

What does formatting actually do to JSON?

Formatting puts each key on its own line and pushes every level of nesting in by the same amount. Here is a record as it might arrive from a server, on one line:

{"name":"Ana","roles":["admin","editor"],"active":true,"profile":{"city":"Lahore","zip":"54000"},"id":12345678901234567890,"price":1.0}

After formatting with 2 spaces:

{
  "name": "Ana",
  "roles": [
    "admin",
    "editor"
  ],
  "active": true,
  "profile": {
    "city": "Lahore",
    "zip": "54000"
  },
  "id": 12345678901234567890,
  "price": 1.0
}

Only the spaces and line breaks between items changed. Minifying this formatted version gives back the one-line original exactly.

Does minifying change the data?

No. The JSON standard, RFC 8259, says whitespace around the structural characters (brackets, braces, colons and commas) is insignificant. Removing it produces the same data. Spaces inside a string, such as the space in "New York", are part of the value and are never removed.

You can test this yourself in under a minute. Format a one-line file, minify the result, and compare it with the original. If the two lines match character for character, only the layout changed. With the sample above they match exactly, and that round trip is a good check for any formatter you are thinking of trusting with real data.

How many bytes does minifying save?

These are the real sizes of the sample above, measured in bytes by our formatter:

VersionSize
Minified135 bytes
Tab indent172 bytes
2 spaces188 bytes
4 spaces220 bytes

For this small record, minified is 53 bytes smaller than the 2-space version, about 28 percent. On real files the saving depends on how deep and how large the data is, so measure your own: the formatter shows the size before and after each action.

Should you indent with 2 spaces, 4 spaces or a tab?

The data is identical either way. The choice affects only the width of the text on screen and the file size:

  • 2 spaces keeps deeply nested data narrow.
  • 4 spaces makes levels easier to tell apart, but long lines grow quickly.
  • A tab is the smallest of the three, and each editor can display it at the width its user prefers.

Use whatever your project already uses. Changing indentation across a whole file makes every line look changed in a diff.

Can a formatter change your values?

It depends on how it works. Some tools parse the text into data and print it again. That step can alter things you never asked to change. In our test, JavaScript's parse-then-print turned 12345678901234567890 into 12345678901234567000 and 1.0 into 1, and a repeated key such as {"a":1,"a":2} came back as {"a":2}. RFC 8259 itself warns that programs differ in how they treat repeated names and very large or precise numbers.

Our formatter works on the original text and changes only whitespace, so numbers, key order and repeated keys stay exactly as written.

When is sorting keys useful?

Sorting keys changes the order of the text and nothing else. The standard describes an object as an unordered collection of name and value pairs, so a program should not depend on key order. Here is the sample with Sort keys (A to Z) turned on:

{
  "active": true,
  "id": 12345678901234567890,
  "name": "Ana",
  "price": 1.0,
  "profile": {
    "city": "Lahore",
    "zip": "54000"
  },
  "roles": [
    "admin",
    "editor"
  ]
}

Sorted keys make it much easier to compare two JSON files, because the same data now lines up the same way. Arrays are never reordered, because their order carries meaning.

You can format, minify and sort keys in one place, in your browser. If the tool reports an error instead, start with fixing errors first. The difference between checking JSON and laying it out is covered in JSON formatter vs JSON validator, and reviewing edits line by line in comparing two versions of a text.

Format and minify: quick questions

What does minify JSON mean?

It means removing every space, tab and line break outside strings, so the JSON becomes one compact line with the same data.

Is pretty print the same as format?

Yes. Pretty print, beautify and format all mean adding line breaks and indentation for people to read.

Can I minify invalid JSON?

Not reliably. Fix the error first. The formatter checks the text and reports the line and column of the first problem.

How large a file can I format?

Up to 500,000 characters. The limit is lower than for the simpler text tools because JSON takes more work to process.

Is my JSON uploaded?

No. Everything runs in your browser and nothing is stored.

Related tools