Text and data formats guide
JSON Trailing Commas and Parse Errors: Locate the Real Problem
Standard JSON does not allow a comma after the last object or array item, but single quotes, unescaped newlines, and mismatched brackets cause similar errors. A formatter narrows the location but does not replace data understanding.
Updated:
Problem
A configuration runs in JavaScript but fails in an API or another language; the error points to the next line, tempting someone to delete valid content. The root is often mixing permissive code syntax with strict JSON.
Who should use this
Useful for developers, analysts, and editors handling API responses, config files, imports, and JSON exchanged between languages.
Formula and concept
Paste a copy into the formatter and keep the original untouched. RFC 8259 defines JSON syntax; trailing commas are not standard JSON, even if a language or editor accepts them permissively.
The reported location may be the token after the real problem. When you see an unexpected character, inspect the preceding comma, quote, or bracket instead of deleting the named line. After formatting, confirm keys and values still have the intended structure.
JSON strings use double quotes, and newlines, backslashes, and control characters need escaping. Comments, `undefined`, `NaN`, and functions are not standard JSON values; serialize or remove code-only syntax from the source.
Formatting checks syntax, not field meaning, units, or personal data. After it passes, compare the API schema, row count, and required fields, and include a source and version when sharing JSON.
For a large file, reduce it to a reproducible section without losing context. Validate the complete file after fixing and keep read-only before/after copies for traceability.
After syntax is fixed, run a small test through the real API staging endpoint or importer. Save the response and fixture version to separate syntax fixes from permissions, field-type, or encoding issues.
If an error appears in only one editor, check whether it uses JSON5 or another permissive syntax. Let the receiving parser define the delivery standard and keep the formatted diff so the same comma is not reintroduced.
Before sharing, remove test personal data and secrets and confirm escaped newlines and backslashes still mean what you intend. Valid JSON can still contain tokens or internal paths; a formatter does not perform a security review.
Finish with a regression test using the consumer’s error handling and a small real-data fixture. Confirm keys, ordering assumptions, and numeric precision did not change; replace the production file only after it passes and keep a recoverable original.
Have a second operator format the corrected file once more and confirm the tool reports no error and produces consistent output before submitting it to production.
Step by step
- Copy the JSON and record its source, version, and parse error.
- Use the JSON formatter to locate the first syntax error.
- Inspect the preceding comma, double quote, and bracket from that location.
- Remove comments, single quotes, unescaped controls, and code-only values.
- After formatting, compare the schema, record count, and required fields.
- Run a small target-API or import test, then save the complete version.
Worked example
A configuration runs in JavaScript but the service rejects line 18. The formatter shows a trailing comma after the final array item on line 17; after fixing it, a single-quoted string appears. The team switches to double quotes, escapes a newline, and tests required fields on a staging endpoint.
Common mistakes
- Deleting only the named line without checking the preceding token.
- Treating a JavaScript object or commented config as standard JSON.
- Using single quotes, `undefined`, or unescaped newlines.
- Delivering after formatting without checking schema or required fields.
- Overwriting the original and losing before/after traceability.
Recommended tools
Related guides
FAQ
- Can JSON have a comma after the last item?
- Standard JSON does not allow a trailing comma after the last object or array item. Some languages accept it, but remove it for cross-system exchange.
- Why is the reported line often not the real problem?
- A parser may fail only when it reaches the next unexpected token, so inspect the previous comma, quote, or bracket. A formatter narrows the range but structure still matters.
- Can JSON contain comments?
- Standard JSON has no comment syntax. If a config tool supports JSON5 or a custom format, convert to standard JSON before sending it to an API and validate again.
Next step
Copy JSON into the formatter, verify syntax with the target API and schema, and keep the original file intact.