Common JSON Errors and How to Fix Them
Published
JSON looks like JavaScript, but its rules are much stricter. Most “invalid JSON” errors come from a handful of habits carried over from code. Here are the most common ones, with the fix for each.
Why JSON is stricter than JavaScript
JSON is a data format defined by RFC 8259, not a programming language. To keep it simple and unambiguous for every language that reads it, it allows only double-quoted strings, a fixed set of values (strings, numbers, objects, arrays, true, false and null) and no extras such as comments or trailing commas. Code that a browser happily accepts as a JavaScript object can therefore be rejected by a JSON parser.
The JSON Formatter checks your JSON as you type and points to the exact line and column of the first problem, with an explanation of the likely cause.
1. Trailing commas
A comma after the last item of an object or array is the single most common error: {"a": 1, "b": 2,} or [1, 2, 3,]. JavaScript allows it, JSON does not.
Fix: delete the comma before the closing } or ].
2. Single quotes and unquoted keys
Strings and property names must use double quotes. {'name': 'Ada'} and {name: "Ada"} are both invalid; the correct form is {"name": "Ada"}.
Fix: replace single quotes with double quotes and wrap every property name in double quotes. If a string itself contains a double quote, escape it as \".
3. Missing commas between items
When you add a line to a file by hand, it's easy to forget the comma at the end of the previous line. The parser then finds a new key where it expected a comma or a closing brace, and reports the error at the start of the new line, not at the end of the old one.
Fix: look at the line before the reported position and add the missing comma.
4. Comments
JSON has no comments: neither // line comments nor /* block */ comments. Configuration files that contain them are usually JSONC (JSON with comments, used by editors such as VS Code) or JSON5, which are different formats.
Fix: remove the comments, or move explanations into a separate "_comment" field if the program reading the file ignores unknown keys.
5. Values that aren't valid JSON
- undefined, NaN and Infinity exist in JavaScript but not in JSON. Use null instead, or store the value as a string.
- Numbers can't have leading zeros (007) or a trailing decimal point (1.). Write 7 and 1.0, or use a string for codes such as postal codes.
- Hexadecimal numbers (0xFF) are not allowed; write the decimal value.
6. Line breaks and tabs inside strings
A string can't span several lines or contain a raw tab character. Text pasted from a document often brings these along.
Fix: write line breaks as \n and tabs as \t inside the string. If you need to send binary data or a whole file inside JSON, encode it with Base64 first.
7. Large numbers that change silently
This one isn't a syntax error, which makes it dangerous. JavaScript stores every number as a 64-bit floating-point value, so integers above 9,007,199,254,740,991 lose precision when parsed: an ID such as 12345678901234567890 becomes 12345678901234567000. Many online formatters change it without warning.
Fix: send large IDs as strings. When you only need to read or reformat JSON, use a tool that keeps the original digits, such as the JSON Formatter.
Quick checklist
- Double quotes around every string and every key.
- Commas between items, but never after the last one.
- No comments, undefined, NaN or leading zeros.
- Escape line breaks (\n), tabs (\t), quotes (\") and backslashes (\\) inside strings.
- Large IDs as strings.