JSON Formatting Tips Every Developer Should Know

From debugging APIs to configuring applications, JSON is everywhere. Master the art of reading, writing, and validating JSON with these practical tips.

JavaScript Object Notation (JSON) has become the lingua franca of data exchange on the web. Whether you're building APIs, configuring tools, or storing data, you'll encounter JSON daily. Here are the essential tips and common pitfalls every developer should know.

JSON Syntax Basics

JSON is strict about syntax. Unlike JavaScript objects, JSON has rigid rules:

✓ Keys Must Be Quoted

All keys must be strings wrapped in double quotes. Single quotes are not allowed.

✓ Strings Use Double Quotes

All string values must use double quotes. 'hello' is invalid; use "hello".

✗ No Trailing Commas

Unlike JavaScript, trailing commas after the last item are syntax errors.

✗ No Comments

JSON does not support comments. Use JSONC (JSON with Comments) for configs that need them.

Common Syntax Errors

1. Trailing Commas

The most common JSON error. That innocent comma after the last item will break everything:

❌ Invalid JSON
{
  "name": "John",
  "age": 30,  ← trailing comma breaks parsing
}
✓ Valid JSON
{
  "name": "John",
  "age": 30
}

2. Single Quotes

Coming from JavaScript or Python? Double quotes only:

❌ Invalid JSON
{
  'name': 'John'  ← single quotes not allowed
}

3. Unquoted Keys

Valid in JavaScript, invalid in JSON:

❌ Invalid JSON
{
  name: "John"  ← key must be quoted
}

Data Type Reference

JSON supports six data types:

{
  "string": "Hello, World!",
  "number": 42,
  "float": 3.14159,
  "boolean": true,
  "null": null,
  "array": [1, 2, 3],
  "object": { "nested": "value" }
}

Note: JSON doesn't support undefined, functions, dates, or special numbers like NaN or Infinity. Dates are typically represented as ISO 8601 strings: "2026-01-31T12:00:00Z"

Formatting for Readability

Pretty Print vs. Minified

Minified JSON saves bandwidth but is impossible to read. Always pretty-print when debugging:

Minified (production)
{"users":[{"id":1,"name":"John"},{"id":2,"name":"Jane"}]}
Pretty-printed (development)
{
  "users": [
    {
      "id": 1,
      "name": "John"
    },
    {
      "id": 2,
      "name": "Jane"
    }
  ]
}

JavaScript Formatting Commands

Format JSON in your code or browser console:

// Pretty print with 2-space indentation
JSON.stringify(obj, null, 2);

// Minify JSON
JSON.stringify(obj);

// Parse and format a JSON string
JSON.stringify(JSON.parse(jsonString), null, 2);

Debugging API Responses

When APIs return unexpected data, follow this debugging workflow:

  1. Validate the JSON: Is it even valid JSON? Paste it into a validator to check for syntax errors.
  2. Pretty print: Format it to see the structure clearly.
  3. Check data types: Is that "123" a string or should it be 123 (number)?
  4. Look for nulls: Missing data often appears as null instead of an error.
  5. Check array lengths: Empty arrays [] might indicate a query returned no results.

Special Characters and Escaping

These characters must be escaped in JSON strings:

{
  "quote": "She said \"Hello\"",
  "backslash": "C:\\Users\\John",
  "newline": "Line 1\nLine 2",
  "tab": "Col1\tCol2",
  "unicode": "Emoji: \u2764"
}

JSON5 and JSONC: When Standard JSON Isn't Enough

Standard JSON is strict for good reason—it's unambiguous. But for configuration files, that strictness can be annoying. Alternatives exist:

JSONC (JSON with Comments)

Used by VS Code, TypeScript, and other tools. Allows // and /* */ comments:

{
  // This is a comment
  "debug": true,
  
  /* Multi-line comments
     are also supported */
  "port": 3000
}

JSON5

A superset of JSON that allows trailing commas, single quotes, unquoted keys, and more. Great for human-edited configs.

Performance Tips

Security Considerations

Format Your JSON Instantly

Beautify, minify, and validate JSON with our free online tool. No signup required.

Open JSON Formatter →

Quick Reference

JSON Checklist:

  • ✓ All keys are double-quoted strings
  • ✓ All string values use double quotes
  • ✓ No trailing commas
  • ✓ No comments (use JSONC for configs)
  • ✓ No undefined, only null
  • ✓ Numbers have no leading zeros (except 0.x)
  • ✓ No single quotes anywhere

JSON's simplicity is its greatest strength. Master these fundamentals, and you'll debug faster, write cleaner configs, and communicate more effectively with APIs. When in doubt, validate—a good JSON formatter will catch mistakes before they become runtime errors.