← All blog posts

JSON Formatter explained with worked examples

WhiteUI · A plain-English guide to the JSON Formatter

The JSON Formatter does one job: it takes JSON text and returns the same data in a readable layout or a squeezed one. The method behind it is two short steps, parse the text into a value, then print that value again with or without indent, and the four worked examples below show what each step gives you from real inputs, with exact counts to check your own results against.

At a glance

9 lines for the sample0816The sample formats to 9 lines
The default sample formats to 9 lines in Pretty mode, out of the 16 lines the nested user record needs.

Example 1: the default sample

The sample already in the box is {"name": "WhiteUI", "tools": ["EMI Calculator", "SIP Calculator"], "free": true, "tools_count": 2}, which is 98 characters as pasted. In Pretty mode it becomes 120 characters across 9 lines:

{
  "name": "WhiteUI",
  "tools": [
    "EMI Calculator",
    "SIP Calculator"
  ],
  "free": true,
  "tools_count": 2
}

Each key sits on its own line, two spaces in, and the two tool names sit four spaces in. In Compact mode the same data becomes {"name":"WhiteUI","tools":["EMI Calculator","SIP Calculator"],"free":true,"tools_count":2}, which is 90 characters on one line. The difference between the two, 120 minus 90, is 30 characters of whitespace, and that is all the formatting adds or removes.

Example 2: a nested order

Take an order record: {"order": 4517, "items": [{"name": "Notebook", "price": 120}, {"name": "Pen", "price": 45}], "paid": true}. It is 106 characters as pasted. In Pretty mode it becomes 162 characters across 14 lines, because the list holds two objects, and each object takes three lines of its own. The prices, 120 and 45, sit six spaces in, at the third level of nesting. In Compact mode it becomes 94 characters on one line. The two item names and the two prices are unchanged in both versions, and the order number, 4517, is still the first value.

Example 3: a user record with two levels of nesting

Try a record with a nested object and a nested list: {"user": {"name": "Anita", "city": "Pune"}, "orders": [{"id": 1, "total": 499}, {"id": 2, "total": 1299}]}. It is 106 characters as pasted, the same length as the order example, but in Pretty mode it becomes 172 characters across 16 lines, ten more characters and two more lines, because the user object adds a level of nesting. In Compact mode it becomes 93 characters. The totals, 499 and 1299, sit six spaces in.

Example 4: a small settings object

Even a tiny object gains lines in Pretty mode. Take {"theme": "dark", "font_size": 14, "notifications": true}, which is 57 characters as pasted. In Pretty mode it becomes 65 characters across 5 lines, one line per key plus the two braces, and in Compact mode it becomes 52 characters. The gain is small, 8 characters, because there is only one level of nesting and no lists. Small objects like this are the ones people most often format by hand, and they are also the ones where a missing comma is easiest to miss, which is why the tool’s validity check earns its keep even here.

What stays the same in every example

In all four examples, three things never change between the input and either output: the keys, the values, and their order. The sample keeps tools_count at 2, the order keeps both prices, and the user record keeps both totals. Formatting only adds or removes spaces and line breaks. That is why the character counts differ while the data does not: the sample is 98 characters in, 120 in Pretty and 90 in Compact, and all three are the same four keys with the same values.

The common mistake

People look at the Pretty output, see that it is longer than the input, 120 characters against 98, and worry that something was added to their data. Nothing was added: the extra 22 characters are spaces and line breaks, and the Compact output proves it, because it holds the same data in 90 characters, 8 fewer than the input. If you want to check, count the keys: the input has 4 top-level keys, and both outputs have the same 4.

Frequently asked questions

How many lines is the formatted sample?

9 lines in Pretty mode, 120 characters. In Compact mode it is 1 line, 90 characters.

What is the pretty output of the order example?

162 characters across 14 lines, from a 106-character input. Compact is 94 characters on one line.

Does formatting change the values in my JSON?

No. Keys, values and order are preserved; only whitespace changes. The sample keeps tools_count at 2 and free at true in both modes.

Why is the nested user record longer than the order example?

Both inputs are 106 characters, but the user record has an extra level of nesting, so Pretty gives it 172 characters and 16 lines against the order’s 162 characters and 14 lines.

More guides on the JSON Formatter

Try it yourself

Try the JSON Formatter — free, in your browser.

Open the JSON Formatter