JSON fundamentals
Common JSON syntax errors and how to fix them
Most invalid JSON comes from a small set of strict syntax rules. This guide shows how to identify each problem, why a parser rejects it, and how to produce a valid replacement without changing the intended data.
Document summary
Valid JSON requires double quoted property names and strings, balanced brackets, supported values, correct escaping, and no trailing commas or comments.
Key takeaways
- Start with the first reported parser error because later errors may be a consequence of it.
- Formatting does not repair invalid syntax unless the tool explicitly provides a repair action.
- After repair, compare the resulting data with the original intent before using it in production.
The rules a strict JSON parser enforces
JSON is a data format, not a JavaScript object literal. It deliberately supports a smaller set of syntax so different systems can exchange the same data reliably.
A document must contain one complete JSON value. Objects use curly brackets, arrays use square brackets, and every opening bracket must have a matching closing bracket.
- Property names must use double quotes.
- String values must use double quotes.
- Allowed values are objects, arrays, strings, numbers, true, false, and null.
- Comments and trailing commas are not part of standard JSON.
Trailing commas after the final item
A comma separates one property or array item from the next. A comma after the final item tells the parser to expect another value, but the object or array closes instead.
{
"name": "Bacodev",
"private": true,
}{
"name": "Bacodev",
"private": true
}Single quotes and unquoted property names
Strict JSON requires double quotes around property names and string values. Single quotes and bare identifiers may be accepted by JavaScript, JSON5, or a custom parser, but they are not standard JSON.
{name: 'Bacodev', private: true}{"name": "Bacodev", "private": true}Unsupported values such as undefined, NaN, and Infinity
JSON supports finite numbers, but it does not support JavaScript values such as undefined, NaN, or Infinity. Choose null, a quoted label, or another valid representation that matches the intended meaning.
| Invalid value | Possible JSON replacement | Important consideration |
|---|---|---|
| undefined | null or omit the property | Null and missing are not always equivalent. |
| NaN | null or a quoted status | A quoted value is a string, not a number. |
| Infinity | A finite limit or quoted label | Confirm how the receiving system expects the value. |
Missing brackets, commas, or colons
A missing delimiter can make the parser report an error near a later line. Check the property or array item immediately before the reported location and verify that every structure closes correctly.
- Every property needs a colon between its name and value.
- Every neighboring property or array item needs a comma.
- Objects and arrays must close in the reverse order in which they opened.
{
"user": {
"name": "Ada"
,
"active": true
}{
"user": {
"name": "Ada"
},
"active": true
}Unescaped quotation marks and control characters
Quotation marks inside a JSON string must be escaped. Literal line breaks, tabs, and certain control characters also need valid escape sequences.
{"message":"She said "hello""}{"message":"She said \"hello\""}Duplicate object keys
Some parsers accept duplicate keys and silently keep the last value. Others reject them or preserve behavior that is difficult to predict. Validating for duplicate keys prevents data from being overwritten unexpectedly.
{"role":"viewer","role":"admin"}{"roles":["viewer","admin"]}A safe JSON repair checklist
Automatic repair is useful, but every repair changes text. Review the result before using it in an API request, configuration file, database import, or production deployment.
- 1
Keep a copy of the original input.
- 2
Repair one class of syntax issue at a time.
- 3
Validate the repaired document.
- 4
Compare important values and data types.
- 5
Test the result in the target system.
Comments inside JSON
Standard JSON has no comment syntax. Remove comments before parsing or use a format that explicitly supports them, such as JSON5, when the receiving system accepts that format.