All posts
Tutorials 3 min read

How to Format and Validate JSON Like a Pro

Learn how to format, validate, and debug JSON quickly. Covers common errors, best practices, and when to use minified vs. pretty-printed JSON.

Bilal jmal
Author

JSON is everywhere — API responses, config files, database exports, webhooks. But raw JSON is often a single unreadable line, and invalid JSON can break your entire application silently. This guide walks through how to format, validate, and debug JSON efficiently, whether you’re working in a browser tool, terminal, or code.

What Is JSON Formatting?

Formatting JSON means adding consistent indentation and line breaks so it’s human-readable. A minified JSON payload like {"name":"Alice","age":30,"active":true} becomes:

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

Both are identical to a JSON parser — but formatted JSON is essential for reading, reviewing, and debugging.

How to Validate JSON

Valid JSON must follow strict rules:

  • All keys must be double-quoted strings — 'single quotes' are invalid
  • No trailing commas after the last item in an object or array
  • No comments (// this breaks JSON)
  • String values must use "double quotes"
  • Numbers, booleans (true/false), and null are unquoted

The fastest way to validate: paste your JSON into our JSON Formatter and click Format / Validate. Errors show with the line number so you can fix them immediately.

5 Most Common JSON Errors

  1. Trailing comma{"a": 1, "b": 2,} — remove the comma after the last item
  2. Single quotes{'key': 'value'} — change to double quotes
  3. Unquoted key{key: "value"} — keys must always be quoted
  4. Missing comma{"a": 1 "b": 2} — add comma between items
  5. Mismatched brackets — an unclosed { or [ — count your opening and closing braces

Pretty-Print vs Minify: Which to Use?

Use formatted (pretty-printed) JSON when:

  • Reading or reviewing data manually
  • Storing JSON in version control (easier to diff)
  • Writing config files or documentation

Use minified JSON when:

  • Sending API responses (reduces payload size)
  • Storing in production databases or caches
  • Optimizing page load where JSON is embedded in HTML

Formatting JSON in the Terminal

If you prefer the command line, jq is the go-to tool:

# Pretty-print a JSON file
cat data.json | jq .

# Minify JSON
cat data.json | jq -c .

# Validate only (exit code 0 = valid)
jq empty data.json

Python also works without any installs:

python3 -m json.tool data.json

Frequently Asked Questions

Why does my JSON pass validation but still break my app?

Syntactically valid JSON can still be semantically wrong for your application — wrong data types (string instead of number), missing required fields, or unexpected nesting. Validation only checks syntax. Schema validation (using JSON Schema) checks structure and types.

What’s the difference between JSON and JSON5?

JSON5 is a superset of JSON that allows comments, trailing commas, single quotes, and unquoted keys. It’s used in some config files (like .babelrc). Standard JSON parsers reject JSON5 syntax — always use standard JSON for API payloads.

How do I format JSON inside a JavaScript file?

Use JSON.stringify(obj, null, 2) to pretty-print with 2-space indentation, or JSON.stringify(obj) to minify. JSON.parse(string) to parse a JSON string back to an object.

All posts
Write a Comment

Leave a Comment

Your email address will not be published. Required fields are marked *