Skip to the content
GigaJSON
All tools
Format and check
View and query
Convert
JSON to code
Encode and decode
Sample files
Guides Open GigaJSON

How to fix invalid JSON: the 10 most common errors

· 7 min read · Updated · By Amir Badi

JSON's grammar is small, and it breaks in a small number of ways. Learn the ten below and most parse errors will tell you where to look. For each one you get the broken input, the error from Node.js (the same V8 engine as Chrome) and from Python, and the fix.

In a hurry? Paste the text into the JSON Repair tool. It fixes most of these and lists each line it changed. The sections below also cover the cases where you shouldn't trust an automatic fix.

GigaJSON removing a trailing comma from a synthetic order objectGigaJSON removing a trailing comma from a synthetic order object (dark theme)
A trailing comma is repairable. Check the resulting fields before using the output. Screenshots follow the page theme. Click to view full size.

Reading the error message first

A parser reports the spot where it gave up, and that's often a line after your mistake. Leave out the comma at the end of line 2 and the error points at the start of line 3, where the parser met a token it didn't expect.

Go to the line and column in the error, then look one token back. To check a file without changing it, the JSON validator shows the exact position.

1. Trailing commas

I see this one more than all the others. It usually comes from copying a JavaScript object, or from deleting the last item of a list by hand.

{
  "name": "Ana",
  "tags": ["a", "b",],
}

Node: Unexpected token ']', ..."["a", "b",],..." is not valid JSON Python: Expecting value: line 3 column 21 (char 39)

Fix: delete the comma before every ] and }. In a big file, a regex search for ,\s*[\]}] finds them all.

2. Single quotes or curly quotes

JSON wants straight double quotes around strings and keys. Single quotes come from Python and JavaScript habits. Curly quotes (“name”) come from word processors and chat apps that "smarten" quotes while you type.

{'name': 'Ana', 'city': 'Porto'}
{“name”: “Ana”}

Node: Expected property name or '}' in JSON at position 1 (line 1 column 2) Python: Expecting property name enclosed in double quotes: line 1 column 2 (char 1)

Fix: replace them with ". Watch out for apostrophes inside values ('O'Brien'). A blind find-and-replace breaks those strings, and a repair tool copes with them better than sed.

3. Unquoted keys

Valid JavaScript, invalid JSON:

{name: "Ana", age: 31}

The error is the same "Expected property name" as above. Fix: {"name": "Ana", "age": 31}.

4. Comments

Config files cause most of these. tsconfig.json and VS Code's settings accept comments because their readers use JSONC, a JSON-with-comments dialect. A strict parser rejects them.

{
  // retry settings
  "retries": 3, /* seconds */ "timeout": 30
}

Node: Expected property name or '}' in JSON at position 4 (line 2 column 3)

Fix: delete the comments, or read the file with a JSONC or JSON5 parser if it's supposed to have them. People who need notes in real JSON usually add a "_comment" key.

5. Missing commas

You'll find these in hand-edited files, and wherever someone pasted two snippets together.

{
  "name": "Ana"
  "age": 31
}

Node: Expected ',' or '}' after property value in JSON at position 20 (line 3 column 3) Python: Expecting ',' delimiter: line 3 column 3 (char 20)

Both messages point at line 3. The comma belongs at the end of line 2: look one token back.

6. NaN, Infinity and undefined

JSON has no NaN, Infinity or undefined. You'll meet them anyway, because Python writes them:

>>> json.dumps({"r": float("nan")})
'{"r": NaN}'

Python reads them back without a word, so the file works fine until someone opens it in JavaScript, Go or a database. JavaScript's JSON.stringify does the opposite and turns NaN and Infinity into null without telling you.

{"ratio": NaN, "max": Infinity, "note": undefined}

Node: Unexpected token 'N', ..."{"ratio": NaN, "max""... is not valid JSON

Fix: decide what the value should mean, which is null most of the time. Some repair tools write the string "NaN" instead. It parses, but the code that reads it may not expect a string. To stop Python writing these, call json.dumps(data, allow_nan=False): it raises an error rather than writing invalid JSON.

7. Python's True, False and None

print(my_dict) or str(my_dict) gives Python syntax, which looks almost like JSON:

{'active': True, 'deleted': False, 'parent': None}

Fix the code that writes it: json.dumps(my_dict) gives {"active": true, "deleted": false, "parent": null}. If all you have is the log line, a repair tool converts the quotes and the constants in one go.

8. Raw line breaks, tabs and bad backslashes in strings

Inside a JSON string, a line break must be written \n and a tab \t. A real newline in the middle of a string is an error:

{"msg": "line one
line two"}

Node: Bad control character in string literal in JSON at position 17 (line 1 column 18) Python: Invalid control character at: line 1 column 18 (char 17)

Backslashes cause a related problem, and Windows paths are where you'll hit it:

{"path": "C:\Users\ana\new"}

Node: Bad escaped character in JSON at position 13 (line 1 column 14) Python: Invalid \escape: line 1 column 13 (char 12)

Fix: double each backslash ("C:\\Users\\ana\\new"), or use forward slashes, which Windows accepts too. Don't lean on automatic repair for this one. A parser can't tell that the \n in \new meant a backslash and an "n" and not a newline, and \U isn't a valid escape at all. When I tried it, jsonrepair (the library behind GigaJSON's repair and several other tools) returned "C:Usersana\new": valid JSON with the wrong data in it. Fix paths by hand. If any of your code builds JSON by gluing strings together, switch it to a real serializer.

9. Truncated JSON

Files get cut off by a full disk, a log line limit, a timeout, or an AI model that ran out of output tokens:

{"items": [{"id": 1, "name": "Ana"}, {"id": 2, "na

Node: Unterminated string in JSON at position 50 (line 1 column 51) Python: Unterminated string starting at: line 1 column 48 (char 47)

Fix: get the whole file again if you can. Failing that, close the open strings and brackets so you can use the rest. A repair tool does this for you. On the example above, jsonrepair returned:

{"items": [{"id": 1, "name": "Ana"}, {"id": 2, "na": null}]}

The last record is incomplete: "na": null is the tool's guess at a cut-off key. Drop that record, or flag it, before anyone treats it as real data.

10. JSON wrapped in an AI answer

Ask ChatGPT, Claude or Gemini for JSON and you often get a sentence, a fenced code block and a closing sentence. Models also slip in trailing commas and // comments more than you'd like.

Sure! Here is the JSON you asked for:

```json
{
  "name": "Ana",
  "skills": ["sql", "go"],
}
```

Let me know if you need anything else.

Node: Unexpected token 'S', "Sure! Here"... is not valid JSON

Fix: take only what's between the fences, then fix the trailing comma. In code, extract the block with a regex before parsing:

const m = /```[\w-]*\s*\n([\s\S]*?)```/.exec(answer);
const data = JSON.parse(m ? m[1] : answer);

If you call the model's API, ask for structured output (most providers offer a JSON mode or a schema option) and you'll have nothing to strip. For an answer you pasted from a chat, GigaJSON's JSON Repair finds the code fence, keeps the block, fixes the comma and shows you the change. Some general-purpose repair setups wrap the surrounding sentences and the JSON into one array instead, so check the shape of what you get back.

What automatic repair can't do

A repair tool guesses what you most likely meant. The cases above are easy guesses. When there's no clear answer, a good tool refuses, as with a doubled comma:

{"a": 1,, "b": 2}

Did someone delete a value, or type the comma twice? jsonrepair stops with Object key expected at position 8, and GigaJSON reports line 1, column 9 without guessing. You fix that one by hand.

Two habits keep repair safe:

  • Read the diff. GigaJSON doesn't repair anything on its own: in the app and the repair tool you choose "Repair", and "See changes" lists each line before and after.
  • Fix whatever writes the file. When the same error keeps coming back, that code is building JSON by hand or calling the wrong function (str() where it needs json.dumps, string concatenation where it needs JSON.stringify). Repairing the output each time just hides the bug.

Quick reference

Error message contains Likely cause
Expected property name or '}' / enclosed in double quotes single quotes, unquoted key, comment, trailing comma before }
Unexpected token ']' / Expecting value before ] trailing comma in an array
Expected ',' or '}' after property value / Expecting ',' delimiter missing comma (look at the line before)
Unexpected token 'N' or 'I' NaN, Infinity
Bad control character / Invalid control character real newline or tab inside a string
Bad escaped character / Invalid \escape single backslash, often a Windows path
Unterminated string / Unexpected end of JSON input / an error at the very last position file cut off (or empty)
Unexpected token 'S', "Sure!..." or similar text around the JSON, such as an AI answer

Once it parses, run it through the JSON formatter: indented JSON makes the next problem easier to spot. In the app, Repair in the Code tab is one undo step, so you can get the broken text back if you don't like the result.

Questions

How do I find where my JSON is broken?

Go to the line and column in the parser's error, then check that spot and the line before it. Parsers often report a missing comma or quote at the next token, one line after the real mistake.

Why does JSON.parse fail on JSON that Python printed?

print() on a dict writes Python syntax, not JSON: single quotes, True, False and None. Use json.dumps(data) to get real JSON. Python's json.dumps also writes NaN and Infinity unless you pass allow_nan=False, and other parsers reject those.

Can invalid JSON be fixed automatically?

Most of it. Repair tools such as jsonrepair handle quotes, commas, comments, Python constants, cut-off files and code fences. Check what changed, though: a repair only guesses what you meant, and a few cases (doubled commas, Windows paths with single backslashes) need a person to decide.

How do I get valid JSON out of a ChatGPT or Claude answer?

Copy only the fenced code block, not the sentences around it, and fix trailing commas or comments if the model added them. A repair tool that recognises Markdown fences, like GigaJSON's JSON Repair, extracts the block for you.

Does JSON allow comments or trailing commas?

No. Standard JSON (RFC 8259) has neither. JSON5 and JSONC allow both, and some tools such as VS Code settings accept them, but JSON.parse and most APIs do not.

More from the blog