·
How to Fix Common JSON Syntax Errors (With Examples) | TextToools
Use parser messages to locate malformed JSON, correct the syntax, and confirm the result still matches what your application expects.
What a JSON syntax error actually means
A message such as “Unexpected token” means a parser could not match the text to JSON grammar at the point it was reading. It does not automatically tell you what the data was meant to be. JSON is a strict, language-independent data format, so JavaScript conveniences such as comments or unquoted property names can fail even when they look familiar. The first goal is to make the text valid JSON, not to guess what the application wanted.
Start with the first parser error. Later messages may be side effects of the same missing character. Error wording differs between browsers, libraries, and command-line tools. Some report a line and column; others show a position or an unexpected character. The location is where the parser recognized a problem, which may be just after the character you actually need to fix.
Keep syntax separate from meaning. A document can parse as JSON and still contain the wrong fields, values, or structure for an API or configuration file. First repair the grammar, then check whether the valid result matches the schema and requirements of the system that will use it.
Check commas between items and after the last item
Objects separate property-value pairs with commas, and arrays separate values with commas. Forgetting a comma often makes the parser point at the next property or value, even though the actual omission is just before it. For example, {"name": "Ada" "active": true} is missing a comma after the string value. Write {"name": "Ada", "active": true} instead.
A comma after the final item is a different, equally common problem. JavaScript and some configuration formats accept trailing commas, but standard JSON does not. The object {"name": "Ada",} and array [1, 2, 3,] are invalid JSON. Remove the final comma: use {"name": "Ada"} or [1, 2, 3].
When fixing a comma error, check the matching pair of braces or brackets rather than deleting punctuation at random. A comma belongs between two items, not before a closing brace or bracket. If you have nested data, format the document with indentation so each item and its container are easier to distinguish.
Use double quotes for property names and strings
JSON property names and string values must use double quotation marks. The JavaScript-style object {'name': 'Ada'} is not JSON, and neither is {name: "Ada"}. The valid form is {"name": "Ada"}. This distinction matters when copying an object literal from source code: the two formats look similar but follow different grammars.
Check that every opening quote has a matching closing quote and that a property name is followed by a colon. A missing quote can make the parser report an unexpected letter much farther along the line. An extra quote can turn what should be a value into the beginning of a new string.
Do not replace every apostrophe in ordinary text. Apostrophes inside a double-quoted JSON string are allowed, as in {"note": "Ada's draft"}. The restriction is about the quotation marks that delimit the JSON string. Preserve the message and change only the delimiters that make the document invalid.
Match braces, brackets, and colons
An object starts with { and ends with }; an array starts with [ and ends with ]. Each opening character needs the correct closing character. A missing bracket near the beginning can make a parser complain at the end of the document, so scan from the reported position back to the nearest unfinished object or array.
Objects need a colon between each property name and its value. For example, {"status" "ready"} is missing the colon, while {"status": "ready"} is valid. A colon in an array, or a comma where a colon should be, can also cause errors that seem unrelated to the surrounding data.
For nested JSON, close each inner structure before its parent. In {"items": [1, 2]}, the array closes before the outer object. If a document is long, inspect one level at a time: match each opening brace or bracket, then confirm that the separator between each key and value is correct.
Escape quotes and backslashes inside strings
A double quote inside a JSON string must be escaped so the parser does not mistake it for the end of the string. For example, the sentence She said "hello" can be represented as {"note": "She said \"hello\"."}. In the JSON text, the backslash marks the quote as part of the value rather than a delimiter.
Backslashes also introduce JSON escape sequences. If a value contains a Windows-style path, each literal backslash must be escaped in the JSON text: {"path": "C:\\reports\\week.json"}. A sequence such as \q is not a valid JSON escape. Common escapes include \n for a line feed, \t for a tab, and \\ for a literal backslash.
A raw line break or other control character cannot appear directly inside a quoted JSON string. If a string was copied from a spreadsheet, log, or multiline editor, check whether a line break split it unexpectedly. Use the appropriate escape when the line break belongs in the value, or keep the text on one line when it does not.
Remove comments and JavaScript-only values
Standard JSON does not support comments. Lines beginning with // or blocks wrapped in /* and */ may be useful in JavaScript or a JSON-like configuration format, but they make a JSON document invalid. If you need a note in a JSON file, use a regular property only when the receiving application permits it; otherwise keep the explanation outside the data.
JSON also has a limited set of values: objects, arrays, strings, numbers, true, false, and null. Values such as undefined, NaN, Infinity, and functions belong to JavaScript, not JSON. Use null only when it accurately represents the intended data; do not replace an unsupported value automatically just to silence the parser.
Numbers have syntax rules too. JSON does not allow a leading plus sign or leading zeroes such as 007, and a decimal point must be followed by digits. Write 7 or 7.0 rather than 007 or 7. These edits make the number valid, but confirm with the receiving system that changing its representation is appropriate.
Use the error location as a clue, not as the whole diagnosis
When a parser gives a line, column, or character position, use it to narrow the search. Look at the reported character and the few characters before it. If the parser says it expected a comma or closing brace, inspect the previous value and the separator that should follow it. The error often appears at the first character the parser cannot accept, not at the original omission.
Long, minified JSON is difficult to inspect because every item appears on one line. Pretty-printing valid input adds indentation and line breaks that make the structure easier to scan. If the document is invalid, fix the first reported syntax problem before expecting a formatter to produce output; a formatter cannot safely infer missing data.
Parser messages are implementation-specific. A browser may phrase an error differently from a server library, and positions can be counted differently when Unicode characters are present. Treat the message as a navigation aid, then verify the exact syntax against JSON rules instead of relying on one error phrase as a universal diagnosis.
Valid JSON is not the same as correct application data
A successful parse proves only that the text follows JSON syntax. It does not prove that a required field is present, that a value has the expected type, or that the data satisfies an API contract. For example, {"enabled": "true"} is valid JSON, but it contains a string rather than the Boolean value true. Whether that is acceptable depends on the system.
Some applications also impose a schema: a required set of property names, allowed values, and rules about nested objects. A generic JSON validator cannot know those requirements unless you provide the schema or use the application's own validation. After fixing syntax, run the data through the tool or service that will consume it.
Duplicate property names are another reason to inspect valid output carefully. The JSON specification says object names should be unique, but software may handle duplicates differently. A parser can accept a document whose repeated keys make the result ambiguous to a person or inconsistent between systems. Keep each object key unique and review the final structure.
A safe workflow for repairing a JSON file
Keep an unchanged copy before editing important data. Then validate the original and note the first reported error. Change one issue at a time—such as adding a missing comma or correcting a quote—and validate again. Small edits make it easier to tell whether a correction solved the problem or introduced a different one.
Once the text parses, pretty-print it to inspect nested objects, arrays, and repeated keys. Compare the formatted output with the source to make sure values were not removed or changed accidentally. Formatting should change whitespace outside strings; it should not be used as a substitute for checking the data itself.
Finally, validate against the destination system. Check required fields, expected types, and any schema or configuration rules. If the file contains credentials or private data, use a trusted local workflow and follow your organization's handling policy rather than pasting it into an unknown service.
TextToools' JSON formatter and validator runs in your browser. Use it to spot syntax errors, choose readable indentation, or minify a valid document when a compact form is specifically needed. It checks JSON syntax, not the rules of your API or application, so keep the destination check as the final step.
A note from the founder
When JSON fails to parse, the fastest fix is usually a careful look at the first error and the characters immediately before it. I built the TextToools validator to make that check straightforward, while leaving the final data and application-specific review to the person who understands the file.
About the author
Written by the founder of TextToools, a collection of straightforward browser tools for working with text.
Sources and further reading
- RFC 8259: The JavaScript Object Notation (JSON) Data Interchange Format — Internet Engineering Task Force
- SyntaxError: JSON.parse: bad parsing — MDN Web Docs