Your API returns a 400 status and the response body says Unexpected token ',' at position 147. The problem is always somewhere in the JSON — but position 147 in a minified payload isn't where you want to go hunting. Here are the 8 errors that cause the vast majority of JSON parse failures, plus how to spot each one before it hits production.
Error 1: Trailing Commas
The single most common JSON error, especially in hand-written configs.
// INVALID
{
"name": "Alice",
"role": "admin",
}
// VALID
{
"name": "Alice",
"role": "admin"
}
JavaScript objects allow trailing commas; JSON does not. If you're generating JSON from a templating system, make sure the last element in any array or object omits the comma. This error shows up as: SyntaxError: Unexpected token '}' at position N.
Error 2: Single Quotes Instead of Double Quotes
JSON requires double quotes for both keys and string values. Single quotes are valid in JavaScript, Python, and most languages — but not in JSON.
// INVALID
{'name': 'Alice', 'role': 'admin'}
// VALID
{"name": "Alice", "role": "admin"}
This often happens when developers copy a Python dict or JS object literal directly into an API body. The error message: SyntaxError: Unexpected token ''' at position 1.
Error 3: Unescaped Special Characters in Strings
Certain characters must be escaped inside JSON strings: double quotes (\"), backslash (\\), newlines (\n), carriage returns (\r), and tabs (\t).
// INVALID — unescaped newline in string
{"message": "Hello
World"}
// INVALID — unescaped backslash
{"path": "C:\Users\alice\documents"}
// VALID
{"message": "Hello\nWorld"}
{"path": "C:\\Users\\alice\\documents"}
Windows file paths are the most common culprit. The parser sees the backslash and expects an escape sequence; when it gets U instead of n, t, r, ", or \, it throws SyntaxError: Bad escaped character.
Error 4: Unquoted Keys
JSON keys must always be strings (quoted). JavaScript allows unquoted keys in object literals, which fools developers copying between JS and JSON.
// INVALID — unquoted key
{name: "Alice", role: "admin"}
// VALID
{"name": "Alice", "role": "admin"}
Error 5: Numbers as Object Keys
Following from the above: numeric keys must also be quoted in JSON.
// INVALID
{1: "first", 2: "second"}
// VALID
{"1": "first", "2": "second"}
Python dicts and JavaScript objects support numeric keys natively; json.dumps() in Python will actually convert them to strings automatically, but if you're serializing manually or using another language, this becomes a parse error.
Error 6: Comments in JSON
JSON has no comment syntax. Neither // nor /* */ are valid.
// INVALID
{
// This is the user config
"name": "Alice"
}
If you need comments in config files, use JSONC (JSON with Comments, supported by VS Code and many tools) or JSON5 — and convert to standard JSON before sending over the wire. The error: SyntaxError: Unexpected token '/' at position 5.
Error 7: Unicode Escape Sequences
Unicode characters above U+FFFF require a surrogate pair in JSON. If you're manually constructing JSON strings with emoji or rare Unicode characters, an incorrect escape sequence causes a parse failure.
// VALID — properly escaped snowman
{"icon": "\u2603"}
// VALID — emoji using surrogate pair (U+1F600)
{"emoji": "\uD83D\uDE00"}
// INVALID — incomplete escape
{"emoji": "\uD83D"}
In practice, you're better off including the actual Unicode character directly (most parsers handle UTF-8 natively) or using a proper serialization library rather than building escape sequences by hand.
Error 8: Incorrect null, true, false Casing
JSON's three literal values are lowercase: null, true, false. Python developers frequently make this mistake because Python uses None, True, False.
// INVALID
{"active": True, "deleted": None}
// VALID
{"active": true, "deleted": null}
Python's json.dumps() handles this correctly, but if you're interpolating Python values into a template string to build JSON, you'll produce invalid output.
How to Debug Efficiently
Three-step process for any JSON parse error:
-
Get the byte offset. Most parsers tell you the position. Paste your JSON into the JSON Formatter & Validator — it highlights the exact line and character.
-
Look before the error, not at it. The parser reports where it got confused, not where you made the mistake. If it says
position 147, the actual error is usually 1-5 characters before that — a missing closing brace, an extra comma, an unescaped backslash. -
Validate before sending. If you're dynamically building JSON strings (instead of using a serialization library), validate the output in your test suite with a strict JSON parser. Never concatenate JSON manually in production code.
Most JSON bugs are caught in under 30 seconds with a formatter that shows line numbers. The ones that take longer are usually the trailing-comma-in-a-minified-payload variant — use the formatter's prettify mode to expand the payload first.
Why JSON Is Strict by Design
JSON's strictness is intentional. Douglas Crockford (who formalized JSON) chose to keep the specification small and unambiguous — no comments, no trailing commas, no unquoted keys, no undefined value. The entire spec fits on a single page. That strictness is why every language and platform can parse it identically: there's no "dialect" of JSON, only valid and invalid.
This is different from JavaScript object literals (which are lenient), Python dicts (which use different quote rules), and YAML (which has a famously complex specification that implementations routinely get wrong in different ways). JSON's rigor is a feature — once you accept it, debugging becomes a matter of finding the single place where you deviated from the spec.
Tools Beyond the Formatter
For debugging JSON in HTTP APIs specifically, three tools speed up the workflow considerably:
Browser DevTools Network tab: Select any request, click Response, and the browser formats the JSON for you. For errors, the Response shows the raw error body. This is the fastest path for debugging API responses during development.
jq command-line tool: For JSON coming through shell scripts or CI pipelines, jq is essential. echo "$API_RESPONSE" | jq '.' pretty-prints any JSON. jq '.error.message' extracts a specific field. jq '. | length' counts array elements. It's faster than switching to a browser tool for server-side debugging.
Postman / Insomnia: For complex API debugging with multiple requests and auth flows, dedicated API clients format responses automatically and highlight syntax errors inline in the request body editor. They also let you paste a raw JSON body and detect errors before sending the request.
Preventing JSON Errors in Production Code
The root cause of most production JSON errors is building JSON by string concatenation instead of using a serialization library. Never do this:
// Dangerous — any value with a special character breaks it
const body = '{"name": "' + userName + '", "role": "user"}';
Instead, let the language serialize the object:
// Safe — the serializer handles escaping automatically
const body = JSON.stringify({ name: userName, role: "user" });
Python's json.dumps(), Go's encoding/json, Rust's serde_json, and every other major language's standard library handles all the escaping, quoting, and type conversion correctly. Building JSON manually is the fastest path to Unexpected token errors at 2 AM.
JSON Formatter & Validator
Paste your JSON to validate syntax, spot errors, and format with line numbers.