← All notes

Data files

How to make JSON easier to read

Use indentation to follow nested data, then check what the values mean.

A compact JSON document can be hard to scan even when it is valid. Indentation separates nested objects and arrays so you can follow their structure.

Start with a small example

Here is a made-up inventory record:

{"item":"Notebook","stock":{"blue":12,"green":7}}

With two-space indentation, the same values are easier to find:

{
  "item": "Notebook",
  "stock": {
    "blue": 12,
    "green": 7
  }
}

Format a simple document in JavaScript

For ordinary JSON with unique keys and numbers that JavaScript can represent accurately:

const value = JSON.parse(text);
const readable = JSON.stringify(value, null, 2);

JSON.parse rejects invalid JSON. Check for trailing commas, missing quotation marks, or mismatched brackets. JSON strings and property names use double quotes.

Keep formatting separate from validation

Successful parsing does not establish that an item exists or a stock count is correct. Check required fields and expected value types separately.

Keep the original when exact numbers or duplicate property names matter. Parsing and reserializing can change numeric precision and discard duplicate keys. A formatter that preserves numeric text is a better choice for those cases.

Use synthetic data when trying an unfamiliar online formatter. Real documents can contain credentials or private information even when the filename looks harmless.

Further reading