Guides5 min read

How to Format and Beautify JSON

Learn how to format, beautify, and validate JSON data. Covers pretty-printing, minification, common errors, and converting between JSON and CSV.

You hit an API endpoint and get back a wall of text. No line breaks, no indentation, just a single line of curly braces and square brackets stretching to infinity. You could squint at it, or you could format it properly and actually see what you're working with.

JSON (JavaScript Object Notation) has become the universal language of data exchange. APIs speak it, config files use it, databases store it. Knowing how to wrangle JSON quickly is a practical skill you'll use daily.

Why JSON Comes Unformatted

When servers send JSON responses, they strip all unnecessary whitespace. A response that looks like this when formatted:

{
  "user": {
    "name": "Alice",
    "email": "alice@example.com",
    "roles": ["admin", "editor"]
  }
}

Gets sent over the wire as:

{"user":{"name":"Alice","email":"alice@example.com","roles":["admin","editor"]}}

This is called minification. It saves bandwidth — removing whitespace from a large JSON payload can reduce its size by 10-20%. That matters when you're serving millions of API requests. But it's terrible for humans trying to read it.

Pretty-Printing JSON

Pretty-printing (or "beautifying") adds consistent indentation and line breaks so you can actually see the structure. The standard is 2 or 4 spaces per indentation level.

Most developers know about JSON.stringify(obj, null, 2) in JavaScript, but that requires you to open a console, parse the JSON first, then stringify it. Our JSON Formatter does it in one step — paste in your minified JSON and get formatted output instantly.

The formatter also catches syntax errors. Forgot a comma? Extra trailing comma? Mismatched brackets? You'll see the error highlighted instead of silently getting undefined behavior.

Common JSON Syntax Errors

JSON is strict. Unlike JavaScript objects, JSON doesn't tolerate:

Trailing commas — This is valid JavaScript but invalid JSON:

{
  "name": "Alice",
  "age": 30,
}

That trailing comma after 30 will break any JSON parser.

Single quotes — JSON requires double quotes. Always.

// Wrong:
{'name': 'Alice'}

// Right:
{"name": "Alice"}

Unquoted keys — Every key must be a double-quoted string:

// Wrong:
{name: "Alice"}

// Right:
{"name": "Alice"}

Comments — JSON has no comment syntax. If you need comments in config files, consider JSONC (JSON with Comments, supported by VS Code) or switch to YAML.

When you paste malformed JSON into the JSON Formatter, it reports the error location so you can fix it quickly.

Working with Nested JSON

Real-world JSON gets deeply nested. An API response from a payment processor might look like this:

{
  "transaction": {
    "id": "txn_abc123",
    "amount": {
      "value": 4999,
      "currency": "USD"
    },
    "customer": {
      "id": "cus_xyz789",
      "address": {
        "line1": "123 Main St",
        "city": "Portland",
        "state": "OR",
        "postal_code": "97201"
      }
    },
    "metadata": {
      "order_id": "order_456",
      "items": [
        {"sku": "WIDGET-01", "qty": 2},
        {"sku": "GADGET-03", "qty": 1}
      ]
    }
  }
}

Without formatting, this is a single unreadable line. With proper indentation, you can immediately see the structure: a transaction containing an amount, customer with address, and metadata with line items.

Converting Between JSON and CSV

Sometimes you need your JSON data in a spreadsheet, or vice versa. These are fundamentally different formats — JSON is hierarchical, CSV is flat — so the conversion involves some trade-offs.

JSON to CSV works best with arrays of flat objects:

[
  {"name": "Alice", "age": 30, "city": "Portland"},
  {"name": "Bob", "age": 25, "city": "Seattle"}
]

Becomes:

name,age,city
Alice,30,Portland
Bob,25,Seattle

Nested objects get flattened with dot notation (address.city) or serialized as strings. Our JSON to CSV converter handles both flat and nested structures.

CSV to JSON goes the other direction, turning tabular data into an array of objects:

product,price,stock
Widget,9.99,150
Gadget,24.99,42

Converts to:

[
  {"product": "Widget", "price": "9.99", "stock": "150"},
  {"product": "Gadget", "price": "24.99", "stock": "42"}
]

Use the CSV to JSON tool when you need to transform spreadsheet exports into API-compatible formats.

When converting CSV to JSON, numbers come through as strings by default. If you need proper number types, you'll want to post-process the output or use a typed converter.

JSON in Everyday Development

Here are the situations where you'll reach for a JSON formatter most often:

Debugging API responses. Copy the response body from your network tab, paste it into the formatter, and immediately see the structure. This beats scrolling through the raw response in DevTools.

Editing config files. Tools like package.json, tsconfig.json, and docker-compose overrides all use JSON. When a config isn't working, formatting it properly often reveals the structural issue.

Writing test fixtures. Building mock data for tests? Start with a formatted template, edit it, then minify for storage if needed.

Comparing payloads. Format two JSON responses identically, then diff them. Inconsistent formatting creates phantom differences — always normalize before comparing.

Minifying JSON

The reverse of formatting — removing all whitespace to create the smallest possible representation. Useful for:

  • Embedding JSON in HTML data attributes
  • Storing JSON in databases where space matters
  • Preparing payloads for manual API calls with curl
  • Reducing log file sizes

Our JSON Formatter supports both directions: beautify for reading, minify for transport.

Try It Yourself

Working with JSON doesn't have to involve firing up a code editor or writing a script every time. These tools handle the common operations instantly:

Everything runs in your browser. Your data stays on your device — nothing gets sent to any server.

Tools Mentioned

Related Articles