Formatting JSON by hand is error-prone. A single missing comma or stray quote breaks the entire parse. This guide shows you how to format JSON correctly — and when a formatter saves you hours of debugging.

Why JSON Formatting Matters

Raw JSON from APIs, config files, or database exports often looks like this:

{"id":42,"name":"Ada Lovelace","roles":["engineer","mathematician"],"active":true}

It’s valid — but unreadable. When you’re debugging or auditing a config, you need structure:

{
  "id": 42,
  "name": "Ada Lovelace",
  "roles": [
    "engineer",
    "mathematician"
  ],
  "active": true
}

Common JSON Formatting Mistakes

Even tools can introduce errors. Watch out for these:

Trailing Commas

{
  "name": "Grace Hopper",
  "language": "COBOL",
}

The trailing comma after "COBOL" is invalid in standard JSON. Always remove the last comma in any object or array.

Mixing Quotes

JSON requires double quotes for all keys and string values. Single quotes are not valid:

{
  'name': 'Alan Turing',    wrong
  "name": "Alan Turing",   correct
}

Numbers as Strings

If a field looks like a number but is quoted, it won’t compare correctly in code:

{
  "userId": "42" string, not number
  "userId": 42 correct type
}

Unicode Escape Sequences

Special characters can sneak in as Unicode escapes that some parsers mishandle:

{
  "greeting": "Hello\u2019s" encoded apostrophe
  "greeting": "Hello's" literal apostrophe (preferred)
}

Best Practices for Formatting JSON

1. Use 2 or 4 Spaces for Indentation

Consistency matters. Pick a standard and apply it project-wide:

{
  "data": {
    "user": {
      "id": 1,
      "name": "Sophie"
    }
  }
}

2. Sort Keys Alphabetically in Config Files

Sorted keys make diffs cleaner and searching faster:

{
  "apiKey": "sk-...",
  "endpoint": "https://api.example.com",
  "timeout": 30000,
  "version": "v2"
}

3. Validate Before Saving

Always validate your JSON before committing or deploying. A single mistake can take down a service.

4. Disable Sorting When Key Order Is Semantically Significant

Some APIs return arrays of objects where order matters. Don’t sort those automatically.

When to Minify JSON

Minification removes all whitespace. Use it only for production payloads where size matters:

{"id":1,"name":"Linus","active":true}

For all other contexts — debugging, code review, documentation — use pretty-printed JSON.

How to Format JSON in Seconds

Rather than formatting by hand, paste your JSON into the JSON Formatter. It handles indentation, validates the structure, and shows you exactly where any errors are.

Summary

JSON formatting errors break parsers silently. The key rules: no trailing commas, double quotes only, correct type for every value. For config files, sort keys alphabetically. Always validate before saving or deploying.

Try the JSON Formatter to clean up any JSON in seconds.