How to Format JSON Like a Pro

Complete guide to JSON formatting: syntax rules, common mistakes, best practices, and real-world examples.

What is JSON and Why Formatting Matters

JSON (JavaScript Object Notation) is a lightweight data-interchange format that is easy for humans to read and write, and easy for machines to parse and generate. Proper formatting -- also called "pretty-printing" -- adds indentation, line breaks, and consistent spacing that makes JSON far more readable than a minified single-line string.

JSON Syntax Rules You Must Know

  • Key-value pairs: Data is stored as "key": value pairs
  • Strings: Always use double quotes ("hello"), never single quotes
  • Numbers: No quotes around numbers (42, not "42")
  • Booleans: Lowercase true and false (not True)
  • Null: Lowercase null (not None or nil)
  • Arrays: Square brackets [1, 2, 3]
  • Objects: Curly braces {"key": "value"}
  • Commas: Separate items with commas, but no trailing comma after the last item

Common JSON Mistakes and How to Fix Them

  • Single quotes: JSON requires double quotes. {'key': 'value'} is invalid. Fix: {"key": "value"}
  • Trailing commas: {"a": 1,} is invalid. Remove the last comma.
  • Comments: JSON does not support // or /* */ comments. Remove them.
  • Unquoted keys: {key: "value"} is invalid. JSON requires {"key": "value"}
  • NaN/Infinity: Not valid JSON. Use null or a string representation.

Real-World Examples

Here is a well-formatted JSON object representing a user profile:

{
  "name": "Jane Doe",
  "email": "jane@example.com",
  "age": 30,
  "active": true,
  "roles": ["admin", "editor"],
  "address": {
    "city": "Prague",
    "country": "Czech Republic"
  },
  "preferences": null
}

Use our JSON Formatter tool to instantly beautify any minified JSON.

Best Practices for Working with JSON

  • Always validate JSON before using it in production
  • Use 2-space indentation for readability
  • Keep JSON files under 1MB for browser performance
  • Use consistent key naming (camelCase or snake_case)
  • Consider JSON Schema for complex data validation

Frequently Asked Questions

What is the difference between JSON and JavaScript objects?
JSON is a text-based data format with strict rules (double quotes only, no functions, no comments). JavaScript objects are more flexible and can contain functions, undefined values, and single-quoted strings.
Can JSON contain comments?
No. Standard JSON (RFC 8259) does not support comments. Some parsers accept them as an extension (JSONC), but it is not standard and will break many tools.
What is the maximum size of a JSON file?
There is no official limit, but browsers typically handle JSON files up to 30-50MB before performance degrades. For larger datasets, consider streaming parsers.
How do I minify JSON?
Remove all whitespace, line breaks, and indentation. This reduces file size for network transfer. Our JSON Formatter tool can both beautify and minify JSON.
Is JSON case-sensitive?
Yes. "Name" and "name" are completely different keys in JSON. Always use consistent casing.