JSON Beautifier
Guide · 5 min read

How to Fix Invalid JSON

JSON syntax errors are frustrating but almost always simple to fix once you know what to look for. Paste your JSON below to repair it in one click, then read on for the 8 most common reasons JSON fails to parse, with exact before/after examples for each one.

Broken JSON
Repaired JSON

Repairs trailing commas, single quotes, unquoted keys, comments, Python's None/True/False, NaN, smart quotes, JSONP wrappers, missing commas and unclosed brackets. Text inside strings is never touched. Everything runs in your browser.

Invalid JSON (3 errors)
Fixed JSON
{
  'name': 'Alice',
  age: 30,
  "city": "New York",
}

Step 1 — Find the Error Line

Before fixing, you need to know where the error is. Paste your JSON into the JSON Validator — it reports the exact line and character position of the first syntax error.

Note: JSON parsers stop at the first error. Fix one error, re-validate, and repeat until the document is clean. There may be multiple errors hidden behind the first one.

The 8 Most Common JSON Errors

These errors account for over 95% of all JSON validation failures:

1Single quotes instead of double quotes

JSON requires double quotes for all strings and keys. Single quotes are valid JavaScript but not valid JSON.

✕ Invalid
{'name': 'Alice'}
✓ Valid
{"name": "Alice"}
2Trailing comma after last item

Remove the comma after the last item in an object or array. Most editors can detect this with a JSON linter.

✕ Invalid
{"a": 1, "b": 2,}
✓ Valid
{"a": 1, "b": 2}
3Unquoted keys

Every key in a JSON object must be wrapped in double quotes. JavaScript objects allow unquoted keys — JSON does not.

✕ Invalid
{name: "Alice", age: 30}
✓ Valid
{"name": "Alice", "age": 30}
4Comments in JSON

JSON does not support comments of any kind — neither // nor /* */. Remove all comments before parsing.

✕ Invalid
{"key": "value" // a comment}
✓ Valid
{"key": "value"}
5Undefined or NaN values

JSON has no undefined or NaN. Replace them with null, or omit the key entirely if the value is not applicable.

✕ Invalid
{"score": NaN, "result": undefined}
✓ Valid
{"score": null, "result": null}
6Missing comma between items

Each key-value pair in an object and each element in an array must be separated by a comma.

✕ Invalid
{"a": 1 "b": 2}
✓ Valid
{"a": 1, "b": 2}
7Mismatched brackets or braces

Every opening bracket [ and brace { must have a matching closing bracket ] and brace }. Use a beautifier to reveal the nesting structure.

✕ Invalid
{"users": [{"id": 1}
✓ Valid
{"users": [{"id": 1}]}
8Numbers as strings (or vice versa)

Numbers should be unquoted (42) and strings should be quoted ("99"). Mixing them causes type errors in most parsers.

✕ Invalid
{"count": "42", "label": 99}
✓ Valid
{"count": 42, "label": "99"}

Common JSON Error Messages, Decoded

If you landed here by pasting an exact error message into search, here's what each one means and how to fix it:

invalid input syntax for type jsonPostgreSQL

What it means: Postgres throws this when inserting or casting a value into a json or jsonb column and the text isn't valid JSON — often a trailing comma, single quotes, or a plain string passed where JSON was expected.

Fix: Validate the string with a JSON validator before the INSERT or UPDATE. If you're building the JSON in application code, use JSON.stringify() (or your language's equivalent) instead of concatenating strings — hand-built strings are the most common cause.

Unexpected end of JSON inputJavaScript — JSON.parse()

What it means: The string passed to JSON.parse() was empty or got cut off partway through — usually a network response that failed, timed out, or was truncated before all the data arrived.

Fix: Log the raw string before parsing to confirm it isn't empty ("") or truncated. If it's an API response, check the request actually completed — a fetch body stream can only be read once.

"undefined" is not valid JSONJavaScript — JSON.parse()

What it means: Something passed the literal string "undefined" to JSON.parse() — usually the result of reading a value that doesn't exist, like localStorage.getItem() returning null and later being coerced to text.

Fix: Check for null before parsing: const raw = localStorage.getItem('key'); if (raw) JSON.parse(raw). Never call JSON.parse() on a value that might be undefined.

Lexical error: invalid char in JSON textStrict parsers (e.g. Java's org.json)

What it means: The JSON contains a raw control character — most often a literal newline or tab typed directly inside a quoted string instead of being escaped.

Fix: Escape control characters inside strings — use \n for a newline and \t for a tab, never the literal character. Run the JSON through a validator to see the exact line and character.

0x0A is invalid within a JSON stringVarious strict parsers

What it means: 0x0A is the hex code for a newline character. A raw line break was found inside a quoted string — the JSON spec requires it to be escaped.

Fix: Find the string containing the line break and replace it with an escaped \n. This is common when JSON is generated from user-submitted multi-line text without proper escaping.

Tips to Prevent JSON Errors

→
Use a JSON-aware editor: VS Code, JetBrains IDEs, and Sublime Text all highlight JSON errors as you type. Enable JSON language support for your files.
→
Validate before committing: Add a JSON lint step to your CI pipeline (e.g. jq . file.json) to catch errors before they reach production.
→
Use JSON.stringify() to generate JSON: Never build JSON strings manually with string concatenation. Always use JSON.stringify() in JavaScript to guarantee valid output.
→
Watch for copy-paste from JavaScript: JavaScript object literals look like JSON but are not. Single-quoted strings and unquoted keys are valid JS but invalid JSON.

Validate and Fix Your JSON Now

Paste your JSON and get the exact error line instantly — free, no sign-up.

Open JSON Validator

Related Guides