Home / Guides / JSON Debugging

Debugging Malformed JSON

JSON is strict. One trailing comma, an unquoted key, or a BOM can break parsers. This guide covers the most common errors and how to fix them.

Common Parse Errors (and what they mean)

Different runtimes produce different messages, but they usually point to the same few root causes. Use this as a quick decoder:

Error message Likely cause Fix
Unexpected token , Trailing comma Remove the extra comma
Unexpected token } / ] Extra closing bracket/brace Find the unmatched bracket
Unexpected end of JSON input Missing closing brace/bracket Add the missing } or ]
Expected ':' after property name Unquoted key, or missing colon Use double quotes, add the colon
Bad control character in string Raw newline/tab in a string Escape with \\n, \\t, \\r

Trailing Commas

JSON does not allow trailing commas. JavaScript does (in most contexts), so if you copy JS object literals into JSON, you may hit this:

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

Fixed:

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

The comma after 30 is invalid. Remove it. Many tools (including our JSON formatter) can auto-repair this.

Unquoted Keys

In JSON, all keys must be double-quoted. This is valid JavaScript but invalid JSON:

{key: "value"}

Fixed:

{"key": "value"}

Single Quotes

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

{'key': 'value'}

Fixed:

{"key": "value"}

Encoding and BOM

JSON should be UTF-8. A UTF-8 BOM (byte order mark) at the start of a file can cause parsers to fail. Some editors add it by default. Strip it, or save as "UTF-8 without BOM." Our formatter handles BOM when present.

Copy/Paste Traps

  • Smart quotes: “quoted” text copied from rich editors becomes invalid JSON. Replace with normal quotes.
  • Invisible characters: zero-width spaces can break parsers. Re-paste into a plain text editor to sanitize.
  • Mixed encodings: ensure the file is UTF-8, especially if it includes non-ASCII characters.

Control Characters

Unescaped control characters (newlines, tabs inside strings, etc.) are invalid. Use \n, \t, and \r instead of raw characters inside string values.

Numbers

Leading zeros are invalid (01). Use 1. Also, 1. or .5 are invalid; write 1.0 or 0.5. Infinity and NaN are not valid JSON.

CLI: jq

For terminal workflows, jq is the standard. jq . file.json pretty-prints and validates. Invalid JSON produces an error. Use our online formatter when you need a quick fix without installing tools.

# Validate (exit 0 if valid)
jq empty < input.json

# Pretty-print
jq . input.json

# Minify / compact
jq -c . input.json

# Extract a field
jq -r '.user.email' input.json

# Filter arrays
jq '.items[] | select(.active == true)' input.json

When Validation Fails

If our validator reports an error, note the line and column. Check for the issues above. When in doubt, try the "Format" button; our repair logic can fix many common mistakes automatically. For production, always validate JSON before processing to avoid security issues like prototype pollution.

Production Validation Checklist

  • Set size limits (do not parse unbounded JSON payloads)
  • Validate structure with a schema (JSON Schema, Zod, etc.)
  • Handle parse errors explicitly; do not silently ignore
  • Avoid merging untrusted objects into application state (prototype pollution)

Editor Tips

Most editors can validate JSON as you type. In VS Code, set the language mode to JSON and consider attaching a JSON Schema for configuration files. For long JSON payloads, formatting in the editor (or in our tool) makes diffs and reviews much easier.

Try the JSON Formatter

Paste your JSON and let our tool validate, format, or repair it. All processing happens in your browser.

Open JSON Formatter