JSON Syntax Error: Common Mistakes and How to Fix Them
A JSON syntax error means the parser hit a character that JSON's grammar doesn't allow at that point. The usual causes are a comma after the last item, single quotes instead of double quotes, keys without quotes, comments, and unescaped characters inside strings. To fix one, go to the line and column in the error message, check that character and the one just before it, and compare them with the short list of rules below.
JSON looks like JavaScript, but its rules are much stricter. The format is defined in RFC 8259 and summarized on json.org. If those documents don't allow something, it's an error, even if your editor or a lenient library accepts it.
This guide covers each common mistake with a broken snippet and a fixed one. It also explains how to read messages like "line 3 column 5 (char 42)" and walks through a debugging routine that works in any language.
The rules every JSON parser enforces
Most JSON syntax errors break one of these rules:
- Strings, including object keys, use double quotes only.
- Object keys must be strings. Bare words and bare numbers aren't allowed.
- A value is a string, number, object, array,
true,false, ornull. The last three are always lowercase. - Commas go between items, never after the last one.
- Comments aren't allowed.
- Numbers are written in decimal. Leading zeros (
07), a leading plus sign, hex,NaN, andInfinityaren't allowed. Exponents like1e3are allowed, and a decimal point needs digits on both sides (0.5and1.0, not.5or1.). - Inside a string,
",\, and the control characters U+0000 to U+001F (tab, newline and the rest) must be escaped. - Between tokens, the only allowed whitespace is space, tab, line feed, and carriage return.
RFC 8259 also requires UTF-8 encoding for JSON exchanged between systems that aren't part of a closed ecosystem.
How to read a JSON syntax error message
The wording depends on the language, the browser, and the version. Most messages have two parts: what the parser expected or found, and where that happened.
Position, line, and column
A typical Python message looks like this:
Expecting property name enclosed in double quotes: line 3 column 5 (char 42)
- Position (also called offset or char) counts from the start of the text, beginning at 0.
- Line and column usually count from 1, so the first character of the file is line 1, column 1.
Browsers vary by engine. Chrome and Edge report a position (and, in recent versions, a line and column). Firefox reports a line and column. Safari often gives only a description with no location.
Offsets count string positions, not bytes. Python counts code points, while JavaScript counts UTF-16 code units, so an emoji counts as two. Neither one matches a byte count in a hex editor.
Why the error often points just past the real mistake
A parser reads from left to right. It only complains when the next character can't possibly be valid, so the reported spot is often one token after the actual mistake. A missing comma gets reported at the start of the next key. Many parsers report a trailing comma at the closing brace. An unclosed string can get reported many lines later, or at the end of the file.
Worked example: a trailing comma
{"a": 1, "b": 2,}
Python 3.12 and earlier report: Expecting property name enclosed in double quotes: line 1 column 17 (char 16).
- Count from 0:
{is char 0,"is 1,ais 2, and so on. The comma after2is char 15, and the}is char 16. - Char 16 is column 17, because columns start at 1. The parser stopped at the closing brace.
- After a comma, the parser expects another key in double quotes. It found
}instead. - The real mistake is one character to the left. Delete the comma and the text parses.
Python 3.13 and later detect this case directly and report Illegal trailing comma before end of object: line 1 column 16 (char 15), which points at the comma itself.
Worked example: a missing comma
{"a": 1 "b": 2}
Python reports: Expecting ',' delimiter: line 1 column 9 (char 8).
- Count from 0:
{is 0,"is 1,ais 2,"is 3,:is 4, the space is 5,1is 6, the next space is 7, and the opening"of"b"is 8. - Char 8 is column 9. After reading the value
1and skipping the space, the parser expected,or}and found a quote instead. - The fix belongs one token back: add a comma right after
1.
The 8 most common JSON syntax errors and their fixes
1. Trailing commas
Broken:
{"name": "Ada", "langs": ["en", "fr",],}
Fixed:
{"name": "Ada", "langs": ["en", "fr"]}
JavaScript and Python both accept trailing commas in their own code, so people carry the habit over. This error also shows up when you delete the last item of a list and forget the comma before it.
2. Single quotes
Broken:
{'name': 'Ada', 'active': True, 'manager': None}
Fixed:
{"name": "Ada", "active": true, "manager": null}
This exact pattern usually comes from printing a Python dictionary with print() or str(). That gives you Python syntax, not JSON. Use json.dumps() instead. Apostrophes inside a double-quoted string are fine: "It's fine".
3. Unquoted keys
Broken:
{name: "Ada", 1: "first"}
Fixed:
{"name": "Ada", "1": "first"}
JavaScript object literals allow bare keys, but JSON doesn't. Number keys need quotes too. If the data starts as a JavaScript object, JSON.stringify() adds the quotes for you.
4. Comments
Broken:
{
// retry count
"retries": 3 /* max */
}
Fixed:
{
"_comment": "retry count",
"retries": 3
}
Some tools accept relaxed variants such as JSON with comments (JSONC) or JSON5, which are common in editor config files. A standard JSON parser still rejects comments. You can delete them, or keep notes in a key your program ignores. If you control both the writer and the reader, you can switch to a format that supports comments.
5. Unescaped quotes, backslashes, and control characters
Broken:
{"quote": "She said "yes"", "path": "C:\Users\docs", "note": "line one
line two"}
Fixed:
{"quote": "She said \"yes\"", "path": "C:\\Users\\docs", "note": "line one\nline two"}
An unescaped " ends the string early, so the parser then trips over yes. A single backslash starts an escape sequence, and \U isn't a valid one. Some broken paths are worse because they parse without any error: "C:\new" silently becomes C:, a line break, and ew. Literal tabs and line breaks pasted from spreadsheets or text editors must also be escaped.
These are the only escapes JSON allows:
| Escape | Meaning |
|---|---|
\" |
Double quote |
\\ |
Backslash |
\/ |
Forward slash (optional) |
\n, \r, \t |
Line feed, carriage return, tab |
\b, \f |
Backspace, form feed |
\uXXXX |
Any character by its four-digit hex code, e.g. \u00e9 for é |
6. NaN, Infinity, and undefined
Broken:
{"score": NaN, "max": Infinity, "bonus": undefined}
Fixed:
{"score": null, "max": null}
JSON numbers must be finite, and undefined isn't a JSON value at all. Pick a replacement: null, a string like "NaN", or leaving the key out. Languages handle these values differently, so the problem is easy to miss:
- JavaScript's
JSON.stringify()turnsNaNandInfinityintonulland drops object properties whose value isundefined. It doesn't throw an error, so data can disappear without warning. - Python's
json.dumps()writesNaNandInfinityby default, andjson.loads()reads them back. That means a file can work fine in Python and then fail in a browser. Passallow_nan=Falsetojson.dumps()to make it raise an error instead.
7. Duplicate keys
Questionable:
{"id": 1, "name": "Ada", "id": 2}
Fixed:
{"id": 2, "name": "Ada", "previous_id": 1}
RFC 8259 says names within an object SHOULD be unique and warns that duplicates make behavior unpredictable. Many parsers, including JavaScript's JSON.parse and Python's json module, quietly keep the last value. Since there's often no error at all, this mistake is harder to catch than the others. Some linters flag duplicates. In Python, you can detect them by passing object_pairs_hook to json.loads().
8. A byte order mark (BOM) at the start of the file
A byte order mark is the invisible character U+FEFF. In a UTF-8 file it's stored as the bytes EF BB BF. Most editors don't show it, so the error seems to point at nothing: position 0, or line 1, column 1.
RFC 8259 says implementations must not add a BOM to JSON sent over a network, and that parsers may choose to ignore one. Many parsers don't. Given a string that starts with a BOM, Python's json.loads() raises Unexpected UTF-8 BOM (decode using utf-8-sig), and JSON.parse() fails on its very first character.
To fix it, re-save the file as plain "UTF-8" rather than "UTF-8 with BOM" (most editors have an encoding option). In Python, open(path, encoding="utf-8-sig") reads files with or without a BOM.
JSON error messages and what they usually mean
These examples come from Python and browser engines. Your exact wording may differ, but the key phrases are similar.
| Message contains | Likely cause | Where to look |
|---|---|---|
| "Expecting value" or "Unexpected end of JSON input" at char 0 | Empty input | The raw response or file content |
| "Unexpected token '<'" at the start | You received HTML (an error or login page), not JSON | HTTP status code and Content-Type header |
| "Illegal trailing comma before end of object/array" (Python 3.13+) | Trailing comma | The reported position is the comma itself |
| "Expecting property name enclosed in double quotes" | Single-quoted or unquoted key, comment; trailing comma (older Python / other parsers) | The character just before the reported spot |
| "Expecting value" mid-text | undefined, single-quoted value; trailing comma in an array (older Python / other parsers) |
The value at the reported spot |
| "Expecting ',' delimiter" or "expected ',' or '}' after property value" | Missing comma, or an unescaped quote ended a string early | The end of the previous value |
| "Unterminated string" | Missing closing quote | The line where the string starts |
| "Invalid control character" or "bad control character" | Raw tab or line break inside a string | Inside the reported string |
| "Invalid \escape" or "bad escaped character" | Single backslash in a path or regex | Backslashes near the spot |
| "Extra data" or "unexpected non-whitespace character after JSON" | Two JSON values back to back, or leftover text | Right after the first complete value |
| "Unexpected UTF-8 BOM" | BOM at file start | File encoding setting |
A step-by-step routine for debugging JSON
- Check that you actually have JSON. Print the first hundred or so characters of the raw text. An empty string, an HTML page, or a value that was already parsed all cause confusing errors.
- Run it through a validator. Paste the text into the JSON formatter, which validates JSON and reports the line and column of the error.
- Go to the reported spot and look one token back. As the worked examples showed, the mistake often sits just before where the parser stopped.
- For minified, single-line JSON, use the position. Print a slice around it, such as
text.slice(Math.max(0, pos - 30), pos + 30)in JavaScript ortext[max(0, pos-30):pos+30]in Python. Themaxguard keeps errors near the start from producing an empty or wrong slice. - Fix one error at a time and validate again. Parsers stop at the first problem, so a second error stays hidden until the first one is fixed.
- Bisect when the location doesn't help. Remove half the items, validate, and keep halving whichever part fails until you find the bad line.
- Look for invisible or look-alike characters. Common culprits are a BOM, non-breaking spaces outside strings, and curly "smart quotes" (“ ”) pasted from word processors or chat apps.
- Fix whatever produced the file. If a script or template generated the bad JSON, correct it there, or the error will come back with the next export.
How to prevent JSON syntax errors
Most of these errors come from writing JSON by hand or building it by gluing strings together. Use your language's serializer (JSON.stringify, json.dumps, or an equivalent), which handles quotes, escapes, and commas correctly. If you're turning spreadsheet rows into JSON, a CSV to JSON converter saves you from typing all the brackets and quotes yourself.
Watch out when JSON gets embedded in other formats. JSON placed in a URL query string must be percent-encoded, as explained in URL encoding explained. JSON has no binary type, so files and images are usually stored as Base64 strings. What is Base64? covers how that works. It also helps to use an editor with a JSON mode that highlights errors as you type, and to validate config files before you deploy them.
Quick checklist for broken JSON
- Double quotes around every key and string.
- No comma after the last item in any object or array.
- No
//,/* */, or#comments. true,false,nullin lowercase. NoTrue,None,NaN,Infinity, orundefined.- Quotes, backslashes, tabs, and line breaks inside strings are escaped.
- No leading zeros or plus signs in numbers, and digits on both sides of any decimal point.
- Every key in an object is unique.
- File saved as UTF-8 without a BOM.
- Error at position 0? Check for empty input, HTML, or a BOM.
- Error mid-file? Look one token before the reported column.
Frequently Asked Questions
What is the difference between a JSON syntax error and a schema validation error?
A syntax error means the text isn't valid JSON, so no parser can read it. A schema validation error means the JSON parsed fine but doesn't match the expected structure, for example a missing required field or a number where a string was expected. Syntax errors have to be fixed first, because schema checks can only run on JSON that parses.
Is a single value like "hello" or 42 valid JSON on its own?
Yes. Under RFC 8259, any JSON value can be the whole document, including a bare string, number, true, false, or null. The older RFC 4627 required an object or array at the top level, so some older tools may still reject a bare value.
Can JSON contain emoji and non-English characters?
Yes. You can write them directly as UTF-8 text, or escape them with \u followed by four hex digits. Characters outside the Basic Multilingual Plane, such as most emoji, need two escapes in a row (a surrogate pair). For example, the grinning face U+1F600 becomes 😀.
How do I store a JSON document inside a JSON string?
Serialize the inner document to a string, then serialize the outer object that contains it. The serializer escapes every inner double quote as " and every backslash as \. Building this by hand is error-prone, so let your language's JSON library do both steps.