Skip to main content
Tools Star Hub

Developer Tools

Common JSON Errors and How to Fix Them

By Tools Star Hub Editorial Team · Published

Quick answer: a JSON parse error means the text breaks one of JSON's strict rules. The most common causes are a trailing comma, single quotes, unquoted keys, comments, a missing comma between values, an unclosed bracket, a raw line break inside a string and values such as NaN or 01 that JSON does not allow. The error's position number counts characters from zero, so position 7 is the eighth character. Paste the text into the JSON Formatter and click Validate: it reports messages such as "Invalid JSON at position 7. Expected double-quoted property name in JSON at position 7 (line 1 column 8)".

A single misplaced character can stop an API request, a build or a config load. Error messages are short and the position can be far from where you made the mistake, so knowing what each message usually means gets you to the fix much faster.

How do I read a JSON error message?

The JSON Formatter uses the browser's JSON.parse and adds the position it reports. The messages shown in this guide come from the V8 engine used by Chrome, Edge and Node.js; Firefox and Safari word them differently, as MDN's JSON.parse error reference shows, but they point to the same problems.

Position is a zero-based character offset. The tool's message also includes the line and column, which count from one. Look at the character at that point and the one just before it: the parser stops where it first realises the text is invalid, which is often just after the real mistake.

Common JSON Errors and How to Fix Them (With Examples) — Tools Star Hub

What do the common error messages mean?

Each row below was produced by running the input through the JSON Formatter's validator.

JSON errors and their causes
InputError message (V8)Cause
{"a":1,}Expected double-quoted property name … position 7Trailing comma
{'a':1}Expected property name or '}' … position 1Single quotes
{a:1}Expected property name or '}' … position 1Unquoted key
{"a":1 // c}Expected ',' or '}' after property value … position 7Comment
{"a":1 "b":2}Expected ',' or '}' after property value … position 7Missing comma
{"a":[1,2}Expected ',' or ']' after array element … position 9Unclosed array
{"a":NaN}Unexpected token 'N'NaN is not JSON
{"a":01}Unexpected number … position 6Leading zero
{"a":1}{"b":2}Unexpected non-whitespace character after JSON … position 7Two values

How do I fix trailing and missing commas?

JSON allows a comma only between items. A comma after the last property or array element, common when you copy from JavaScript, triggers "Expected double-quoted property name" or a similar message. Delete the final comma.

A missing comma between two properties produces "Expected ',' or '}' after property value". Check the line where the position falls; the previous value usually needs a comma after it.

How do I fix quote and key errors?

Every key and every string must use double quotes. Text copied from Python dictionaries or JavaScript objects often uses single quotes or bare keys, which both produce "Expected property name or '}'". Replace single quotes with double quotes and wrap keys such as a in quotes: "a".

If a string itself contains a double quote, escape it as \". A backslash must also be escaped as \\, which matters for Windows paths.

Why does a line break inside a string fail?

JSON strings cannot contain raw control characters such as a newline or tab. A value that spans two lines gives "Bad control character in string literal". Replace the line break with the escape \n, or a tab with \t.

This often happens when someone pastes multi-line text into a config value, or builds JSON by joining strings instead of using a serializer. Building data as objects and calling JSON.stringify avoids it.

Which values are not allowed?

JSON numbers cannot have leading zeros, so 01 gives "Unexpected number". NaN, Infinity and undefined are JavaScript values, not JSON, so they produce "Unexpected token". Use null or a string instead, and remove leading zeros or store codes such as 01 as strings.

A JSON text is exactly one value. Two objects back to back, as in newline-delimited logs, give "Unexpected non-whitespace character after JSON". Wrap them in an array or validate one line at a time.

How do I debug JSON errors from an API?

When JSON.parse fails on a network response, first look at the raw body rather than the parsed result. A message that starts with "Unexpected token '<'" usually means the server returned an HTML page, such as a login screen, error page or proxy notice, instead of JSON.

Check the HTTP status code and the Content-Type header. A 200 response with application/json that still fails to parse often has been cut off, which gives an unexpected end of input error, or has extra text before or after the JSON. Copy the body into the JSON Formatter to see exactly where it breaks. If the body is fine there, the problem is in how your code reads it, for example parsing it twice or parsing an object that is already parsed.

How can I avoid JSON errors in the first place?

Generate JSON with a serializer instead of writing it by hand or joining strings. JSON.stringify in JavaScript, json.dumps in Python and similar functions in other languages always produce double quotes, escape special characters and omit trailing commas.

For files people edit, such as config or translation files, run a validator in your editor or build so that a stray comma is caught before deployment. If you need comments in a config, consider a format that allows them, such as YAML, rather than putting comments in JSON.

What if the JSON file is very large?

The JSON Formatter accepts up to 750,000 characters, which covers most API responses and config files. For larger exports, validate a smaller sample first, or split newline-delimited records and check them one at a time.

Large files make the position number more useful than scrolling. Note the line and column from the error message, open the file in an editor, and jump straight to that line to see the character that broke the parse.

How do I find and fix a JSON error?

  1. Paste the text. Open the JSON Formatter and paste the JSON, up to 750,000 characters.
  2. Click Validate. Read the message, position, line and column.
  3. Fix the character. Look at the reported spot and the character before it. Apply the matching fix from the table.
  4. Validate again. Repeat until the tool says "This JSON is valid.", then format or minify it.

Example

Trailing comma

{"a":1,} becomes {"a":1}.

Python-style dictionary

{'a': True} becomes {"a": true}, because JSON uses double quotes and lowercase true.

Multi-line string

A value with a real line break becomes "line\nbreak".

What should I keep in mind?

Position counts from zero

Position 7 is the eighth character; the tool also shows line and column from one.

Browsers word errors differently

The cause is the same even when Firefox or Safari phrase the message another way.

Empty input

An empty box returns a prompt to paste JSON, not a parse error.

Valid is not the same as correct

Validation checks syntax only, not whether fields have the values your app expects.

FAQ

What does Unexpected token mean in JSON?
The parser found a character that cannot appear at that point, such as N from NaN or a letter starting an unquoted value.
What does Unexpected end of JSON input mean?
The text stopped before every object, array or string was closed. Look for a missing } or ] or a cut-off response.
Why is a single value such as "hello" valid JSON?
RFC 8259 allows any JSON value at the top level, so a string, number, true, false or null on its own is valid.
Can JSON contain comments?
No. Remove // and /* */ comments, or move notes into a regular field.
Why does my API return HTML instead of JSON?
An error page or login redirect often returns HTML, which fails at the first < character. Check the response status and content type.
How do I escape a double quote inside a JSON string?
Write a backslash before it: \".
Are true, false and null case-sensitive?
Yes. JSON accepts only lowercase true, false and null.
Why does a number with a leading zero fail?
JSON forbids leading zeros except for 0 itself, to avoid confusion with octal. Store codes such as 007 as strings.
How do I validate newline-delimited JSON?
Validate one line at a time, because each line is a separate JSON value.
Does the formatter fix errors automatically?
No. It reports the first error and its position so you can fix the text yourself.
How do I find the line that broke my JSON parse?
Read the parser’s line and column hint, open that spot, and fix commas, quotes, or brackets there.
Why do single quotes around keys fail?
JSON requires double quotes for keys and strings; replace single quotes before parsing.
What fixes an unescaped backslash in a path?
Double the backslash or switch to forward slashes so the string stays valid JSON.
How can trailing commas sneak in from JavaScript style?
JS allows trailing commas in objects; JSON does not—delete the comma after the last property.
Why does capital True fail while true works?
JSON literals are lowercase true, false, and null only; other capitalizations are invalid.
What should I do when the response is an HTML error page?
Inspect status codes and Content-Type; fix the endpoint so it returns application/json instead of an HTML body.
How do I encode newlines inside a string value?
Use \n inside the JSON string rather than a raw line break unless you are using a relaxed format.
Can duplicate keys cause confusing results?
Parsers may keep the last duplicate silently; remove duplicates so intent is clear.
How do I validate NDJSON differently from one JSON document?
Parse each line as its own JSON value; a wrapper array is a different shape than newline-delimited rows.
What is a quick check before sending JSON to an API?
Run it through a strict formatter or parser locally and confirm required fields and types.

Paste your JSON and click Validate in the JSON Formatter.

To compare JSON with other formats, read JSON vs XML vs YAML.

To inspect a token's JSON payload, use the JWT Decoder.

To convert rows to valid JSON, use CSV to JSON.

Sources and further reading