Guide
Common errors when writing JSON
Identify and fix invalid commas, quotes, comments, escapes, numbers, and closings with before and after JSON examples.
by Tools in a Tab · Published on · Updated
Short answer
Many JSON errors follow a few patterns: a missing or extra separator, a string with incorrect quotes or escapes, a JavaScript-only value, or an incomplete structure. Recognizing the pattern helps you correct the cause without rewriting the entire document.
Use this reference to compare invalid inputs with their corrected forms. To locate a problem in your own text, use the JSON validator. To interpret its line, column, and context, follow the procedure to validate JSON and locate the error.
Quick reference
| Pattern | Typical clue | Minimal correction |
|---|---|---|
| Trailing comma | There is a , before ] or } |
Remove the comma |
| Missing comma | Another property or element starts without a separator | Add , between the values |
| Missing colon | The value appears immediately after the name | Add : between name and value |
| Unquoted property | The name looks like a JavaScript identifier | Enclose it in " |
| Single quotes | The string or property uses ' |
Use double quotes |
| Invalid escape | A backslash starts an unknown sequence | Escape the backslash or use a JSON escape |
| Comment | // or /* ... */ appears |
Remove comment |
| Unsupported value | undefined, NaN or Infinity appears |
Model the data with JSON values |
| Invalid number | There is a leading zero or missing digits | Correct the number or use a string |
| Unexpected ending | Missing value or closure | Complete the structure |
| Extra content | There are two consecutive root values | Keep one or group them |
Parsers do not have to word their messages the same way. The reported position may be the character where parsing can no longer continue even when the cause is immediately before it.
Separators: commas and colons
The colon separates a property’s name from its value. Commas separate members of an object or elements of an array. They are not interchangeable and a comma never closes a list.
A comma just before a closing bracket or brace
This list has a comma after its last element:
{"roles":["editor","reader",]}
JSON does not support trailing commas. The valid version ends the last value and closes the array directly:
{
"roles": ["editor", "reader"]
}
The same rule applies to objects: there cannot be a comma before
}.
Missing comma between properties
Here "port" starts without a comma after the previous member:
{"host":"localhost" "port":8080}
The fix belongs between "localhost" and the following property:
{
"host": "localhost",
"port": 8080
}
The parser can point to the beginning of "port" because that is the point
where it discovers that it cannot continue. The missing character is immediately before it.
The colon is missing
In an object, the name must be followed by a colon before the value:
{"mode" "safe"}
Add the colon:
{
"mode": "safe"
}
A short rule helps differentiate them: : matches a name with its value; ,
separates that pair from the next.
Properties, quotes and strings
JSON looks like JavaScript object notation, but it doesn’t accept all of its abbreviated forms. Both property names and strings are delimited with double quotes.
A property name does not have quotes
This name could work as an identifier in JavaScript, but it is not a JSON string:
{mode:"safe"}
It must be written between double quotes:
{
"mode": "safe"
}
The rule also applies to numeric or hyphenated names: within a JSON object, every property name is a string.
Single quotes have been used
Single quotes do not delimit strings or properties in JSON:
{'active':true}
The valid version uses double quotes:
{
"active": true
}
Do not replace all single quotes automatically. An apostrophe can be part of a string’s content and does not need escaping in JSON:
{
"message": "The user's session is open"
}
A backslash starts an invalid escape
In a string, the backslash \ starts an escape sequence. That’s why this
path contains \d and \i, which are not JSON escapes:
{"path":"C:\data\input"}
To represent a literal backslash you have to write two:
{
"path": "C:\\data\\input"
}
After parsing the JSON, each \\ represents one backslash in the value.
JSON also supports short escapes such as \n, \t, \r, \b, and \f, in addition
to \" for an inner quote and \uXXXX for a UTF-16 code unit.
A literal line break or tab inside a string is a control character and must be escaped. Outside strings, spaces, tabs, carriage returns, and line feeds can serve as whitespace where the grammar permits it.
Forms of JavaScript that JSON does not support
A file may look like JSON while using JavaScript, JSON5, JSONC, or an application-specific extension. A permissive parser may accept those features, but that does not make them standard JSON.
Comments
JSON does not define line or block comments:
{
"port": 8080, // HTTP port
"secure": false
}
If the comment only documents the file, the correction is to remove it:
{
"port": 8080,
"secure": false
}
If the note is to be part of the data, it could be modeled as a property
only when the receiving system allows it. Adding a "comment" property without
checking may violate the API contract.
undefined, NaN and Infinity
These values exist in JavaScript, but are not part of the JSON grammar:
{"result":NaN,"optional":undefined}
JSON supports objects, arrays, strings, numbers, and the three literals
true, false and null. A possible representation would be:
{
"result": null,
"optional": null
}
null is not an automatic replacement. Depending on the meaning of the data,
you may need null, an omitted property, or a different representation of the
state. The receiving system’s contract determines which is correct.
Numbers that do not comply with grammar
JSON numbers are written in base ten. They may include a negative sign, a fraction and an exponent, but do not admit leading zeros unless the integer part consists of a single zero.
This value is not valid as a number:
{"attempts":03}
If it represents a quantity, the zero is removed:
{
"attempts": 3
}
If 03 is a code and the zero has meaning, it must be preserved as text:
{
"attempts": "03"
}
A fraction needs at least one digit after the period and an exponent
needs digits after e or E and any exponent sign. That makes 1. and 2e
incomplete JSON numbers. A leading + is not allowed, although an exponent
can contain one, as in 1e+3.
Incomplete documents or multiple root values
The entire JSON text contains a single root value. That value must be finished before the end of the document and cannot be followed by a second independent value.
The document ends before a value is complete
Here an array is opened but neither the array nor its surrounding object is closed:
{"items":[
If the intention was to render an empty list, the minimum fix is:
{
"items": []
}
An unexpected ending can also indicate an unclosed string, a missing value
after :, or an unclosed object. Complete the structure according to the
intended data; do not add arbitrary brackets or braces merely to silence the
parser.
There are two consecutive root values
These are two separate objects without any common container:
{"ok":true}{"ok":false}
If both belong to the same document, they can be grouped in an array:
[
{
"ok": true
},
{
"ok": false
}
]
You could instead keep one object or process each document separately. Grouping them is only correct if the receiver expects a list.
Three cases that are not syntax errors
Avoiding these false positives is as important as recognizing an incorrect comma or quote.
A root value can be primitive
"text", 42, true, and null are valid JSON documents. An API can
require that the root be an object or an array, but that is a rule of its
contract, not the general JSON syntax.
Permitted whitespace is valid
Spaces, tabs, carriage returns, and line breaks may appear outside strings at the points allowed by the grammar. Formatting a document changes its presentation, not its validity.
Duplicate names are a different problem
This text complies with the grammar, although it repeats the same name:
{
"status": "pending",
"status": "shipped"
}
RFC 8259 recommends unique object names because readers may keep the first value, the last value, every occurrence, or reject the document. The Tools in a Tab syntax validator accepts this example. Its formatter and JSON-to-YAML converter preserve the repeated names, while JSON-to-CSV rejects them. Resolve the duplication explicitly; a successful syntax check does not settle which value is intended.
Check the correction
After recognizing a pattern, change only the identified cause and revalidate the entire document. The JSON validator preserves your input and shows the first problem with line, column and context directly in this tab.
When the result is already valid, you can use the JSON formatter to improve readability or generate a compact version. Valid syntax does not guarantee that an API will accept the data: check required properties, types, formats, and business rules in its documentation or JSON Schema.
Technical reference
The grammar and interoperability recommendations used in this guide are defined in RFC 8259. The invalid examples are checked against the Tools in a Tab validator and the corrections against the native JSON parser.