When JSON will not parse—and when parsing changes the data
A small edit to an API response can turn valid JSON into an error. Its resemblance to JavaScript makes it easy to borrow syntax that JSON does not accept. Start with punctuation and types, then check the quieter failures: a duplicate key disappearing or an identifier changing its last digit.
Look just before the reported error
A parser may only notice a missing quote when it reaches the next field. The reported position is a useful starting point, but the previous value and its closing punctuation often explain the problem.
Common repairs
| Invalid input | Valid replacement | What changes |
|---|---|---|
{name: "Mika"} |
{"name": "Mika"} |
Quote the property name |
{'count': 2} |
{"count": 2} |
Use double quotes |
{"count": 2,} |
{"count": 2} |
Remove the trailing comma |
{"enabled": True} |
{"enabled": true} |
Use lowercase literals |
{"count": 01} |
{"count": 1} |
Remove the extra leading integer zero |
Comments belong to a different format
Ordinary JSON has no comment syntax. An editor that accepts JSONC does not make those comments acceptable to a JSON API. Keep notes separately, or use a description field if the receiving application permits one.
Choose a type for the value’s meaning
A product code such as 0012 needs its leading zeros; a quantity of 2 usually needs arithmetic. Quoting the code preserves its spelling, but quoting the quantity may violate an API that expects a number. A successful parse does not establish that a field has the right meaning.
A small record to try
Here code is a string, count is a number, enabled is a boolean, note is null and tags is an array. Null is neither an empty string nor a missing property. An update API might use an omitted note to mean “leave it alone” and null to mean “clear it”; that is an application contract, not a rule shared by every JSON consumer.
{"code":"0012","count":2,"enabled":false,"note":null,"tags":[]}
Represent a line break inside a string as \n
A literal line break between the opening and closing quotes is not valid JSON string syntax. Write \n for that character. A backslash that is part of a Windows path also needs escaping.
Input and decoded values
After parsing, message has a line break between first and second, and path contains C:\temp\sample.txt. If you place this entire JSON document inside another JSON string, that outer layer needs its own escaping. Identify which string layer you are editing before removing backslashes.
{"message":"first\nsecond","path":"C:\\temp\\sample.txt"}
A repeated key can disappear without an error
Consider {"mode":"test","mode":"live"}. RFC 8259 §4 says names SHOULD be unique; it does not give every parser the same result for duplicates. JavaScript JSON.parse keeps the later value. A syntax check alone therefore cannot establish that the original data had no duplicate names.
Keep the original text before formatting
Online Tools JSON Formatter uses JSON.parse, so formatting this example leaves only mode with the value live. The formatted result cannot tell you what was discarded. JSON Diff rejects repeated names; putting the original text into both sides of that tool is one way to detect this particular problem.
A changed final digit points to numeric precision
JSON number syntax does not impose JavaScript’s storage precision. The usual JavaScript Number type can represent every integer exactly only within ±9,007,199,254,740,991. Some larger integers are representable, but consecutive values are no longer all distinct. An identifier often belongs in a string, provided the sender and receiver agree on that type.
Reproduce the change in JavaScript
The Online Tools formatter and JSON-to-CSV converter also use JSON.parse. Preserve the source when handling long identifiers, and compare the resulting digits. Changing a number to a string can affect an API contract, so it is a data-model decision rather than an automatic repair.
JSON.stringify(JSON.parse('{"id":9007199254740993}'))
// {"id":9007199254740992}
JSON.stringify(JSON.parse('{"id":"9007199254740993"}'))
// {"id":"9007199254740993"}
Test the small example, then return to the full document
Paste a short example into JSON Formatter and compare formatting with validation. A successful validation means the parser accepted the syntax; it is not JSON Schema validation or a check of business rules.
Related reading
For changes between versions, see JSON object order, array order and diffs. For a format decision, see Choosing data formats; for garbled text, see Text encoding basics.
Specification references
See RFC 8259 §§2–3 for syntax and values, §4 for duplicate names, §6 for number interoperability, and §7 for strings. The ECMAScript JSON.parse specification defines the JavaScript operation.