How to Fix Invalid JSON: Common Errors and Solutions
JSON is notoriously strict. A single misplaced comma, a single-quoted string, or a JavaScript comment can make an entire document unparseable. Broken JSON shows up everywhere: copy-pasted from browser consoles, generated by AI models, extracted from log files, or written quickly by hand.
This guide covers every common JSON error with exact examples and fixes, plus how to repair invalid JSON automatically using IRJSON.
Common JSON Errors at a Glance
| Error Type | Example (Broken) | Fix |
|---|---|---|
| Trailing comma in object | {"a":1,"b":2,} | Remove the trailing comma |
| Trailing comma in array | [1, 2, 3,] | Remove the trailing comma |
| Single-quoted strings | {'name':'Alice'} | Use double quotes |
| Unquoted keys | {name: "Alice"} | Quote the key: "name" |
| undefined value | {"x": undefined} | Replace with null |
| NaN value | {"x": NaN} | Replace with null |
| Infinity | {"x": Infinity} | Replace with null |
| Single-line comment | // comment
{"a":1} | Remove comment |
| Block comment | /* note */{"a":1} | Remove comment |
| Missing closing brace | {"a":{"b":1} | Add closing } |
Trailing Commas
Trailing commas are the most frequent JSON error. They are valid in JavaScript and Python but not in JSON.
// Invalid JSON — trailing comma after last property
{
"name": "Alice",
"email": "alice@example.com",
}
// Fixed
{
"name": "Alice",
"email": "alice@example.com"
}
Trailing commas in arrays are equally invalid:
// Invalid
["red", "green", "blue",]
// Fixed
["red", "green", "blue"]
If you frequently write JSON with trailing commas, consider using JSON5 for config files (it explicitly allows them) or enabling your editor's JSON5 language mode.
Single-Quoted Strings
JSON requires double quotes for all strings — both keys and values. Single quotes are valid in JavaScript but not in JSON.
// Invalid JSON — single quotes
{'name': 'Alice', 'role': 'admin'}
// Fixed
{"name": "Alice", "role": "admin"}
Unquoted Object Keys
JavaScript allows bare identifier keys in object literals. JSON requires all keys to be double-quoted strings.
// Invalid JSON — unquoted keys
{name: "Alice", age: 30}
// Fixed
{"name": "Alice", "age": 30}
undefined, NaN and Infinity
These JavaScript-only values have no JSON equivalent. Replace them with null or a meaningful fallback.
// Invalid JSON
{
"result": undefined,
"ratio": NaN,
"limit": Infinity
}
// Fixed
{
"result": null,
"ratio": null,
"limit": null
}
Comments in JSON
Standard JSON (RFC 8259) does not allow comments of any kind. Both JavaScript-style single-line (//) and multi-line (/* */) comments cause parse errors.
// Invalid JSON — comments are not allowed
{
// User configuration
"name": "Alice",
/* These are her roles */
"roles": ["admin"]
}
// Fixed
{
"name": "Alice",
"roles": ["admin"]
}
If you need comments in your config file, switch to YAML (which supports # comments) or JSON5 (which supports both comment styles). VS Code uses JSONC (JSON with Comments) for its own settings files.
Missing Closing Brackets
Unclosed braces or brackets are common when manually editing large JSON files. They are hard to spot by eye but easy to detect with a validator.
// Invalid JSON — missing closing }
{
"user": {
"name": "Alice",
"settings": {
"theme": "dark"
}
// missing closing brace here
// Fixed
{
"user": {
"name": "Alice",
"settings": {
"theme": "dark"
}
}
}
Fix Invalid JSON Online with IRJSON
IRJSON's repair tool fixes all the errors above automatically. Paste your broken JSON and the repaired version appears instantly with a list of every change made:
Fixing Invalid JSON in JavaScript
function safeParseJson(str) {
try {
return { data: JSON.parse(str), error: null }
} catch (e) {
return { data: null, error: e.message }
// e.message: "Unexpected token } in JSON at position 47"
}
}
const result = safeParseJson('{"name": "Alice",}')
if (result.error) {
console.error('JSON parse failed:', result.error)
// Handle: show error to user, try repair, log for debugging
}
Fixing Invalid JSON in Python
import json
def safe_parse_json(text):
try:
return json.loads(text), None
except json.JSONDecodeError as e:
return None, str(e)
# e.g. "Expecting ',' delimiter: line 3 column 1 (char 28)"
data, error = safe_parse_json('{"name": "Alice",}')
if error:
print(f"JSON error: {error}")
# Use a repair library or fix manually
Frequently Asked Questions
Why is JSON so strict about trailing commas?
The JSON spec (RFC 8259) was designed to be simple and unambiguous. Trailing commas were deliberately excluded to keep parsers minimal. JavaScript allows trailing commas in object and array literals but JSON does not — this is one of the most common sources of confusion for developers coming from JavaScript.
Can I use JSON5 instead of JSON?
JSON5 is a superset of JSON that explicitly allows trailing commas, comments, single-quoted strings and more. It requires a dedicated parser — standard JSON.parse rejects it. JSON5 is a good choice for hand-written config files. It should not be used for API data exchange where the client expects standard JSON.
What is the jsonrepair library?
jsonrepair is an open-source JavaScript library (also available as a CLI) that automatically fixes common JSON errors. IRJSON's repair tool uses jsonrepair under the hood. You can use it directly in Node.js: import { jsonrepair } from 'jsonrepair'.
Fix invalid JSON automatically
IRJSON's repair tool fixes trailing commas, single quotes, unquoted keys, comments and more with a single click.
Open JSON Repair Tool →