Guides / JSON syntax
Fix JSON Comments and Trailing Commas Before CSV Conversion
Keep the original file, locate the parser error, and correct a separate copy before converting. Comments outside strings and commas immediately before a closing object or array are not standard JSON. Remove only the syntax you have reviewed; do not run a blanket replacement over URLs or text inside quotes. Parse the corrected copy again, then check its CSV values against that copy.
Why the converter rejects an otherwise readable export
A configuration-style export can look understandable while failing a JSON decoder. The JSON grammar in RFC 8259 has no comment token. Its object and array rules require a value or member after a separator. A trailing comma therefore cannot simply stand before the closing bracket or brace.
This is a syntax problem before row selection or CSV formatting. Raising a file-size setting, changing the CSV delimiter or renaming the input file will not turn that syntax into JSON. If the source is intentionally a different format, ask its producer for standard JSON or use a parser designed for its declared format before handing data to a JSON to CSV converter.
An original example that makes blind cleanup unsafe
The source below contains an actual comment line, an object trailing comma and an array trailing comma. It also contains three things that must survive: https://, a literal /* literal */ string, and the characters ,] inside a note.
[
// Export note: review the two records before conversion.
{"id":"R01","url":"https://example.com/a","note":"Keep /* literal */ and ,] as text",},
{"id":"R02","url":"https://example.com/b","note":"She said \"ready\"; not // a comment"},
]
A rule that deletes everything after // can cut a URL or quoted note. A rule that strips block comments can remove the literal note. A rule that replaces every ,] can alter the string even if it fixes an array elsewhere. Valid syntax alone is insufficient evidence that those replacements preserved the data.
A reviewable correction, not an automatic repair
- Save the original bytes under a new evidence name; do not overwrite them.
- Read the first reported line and column, then inspect the surrounding quoted strings and structural characters.
- For this fixture, remove the comment line outside strings and the two structural trailing commas.
- Save a separate corrected file and keep a diff showing exactly those edits.
- Parse the corrected file again; then compare the expected records and field text before exporting CSV.
[
{"id":"R01","url":"https://example.com/a","note":"Keep /* literal */ and ,] as text"},
{"id":"R02","url":"https://example.com/b","note":"She said \"ready\"; not // a comment"}
]
The complete edit diff is downloadable below. It records changes to syntax outside the string values. It is an example of a deliberate review, not a claim that the tool can discover the intended meaning of arbitrary broken JSON.
Run a read-only syntax gate
The Python decoder error documentation describes line and column information for a JSON decoding failure. The gate reports that information when available, plus the SHA-256 of the exact input bytes. It does not alter either file. It stops at the first decoding error; an error location is where parsing failed, not a complete list of all defects.
python json-syntax-gate.py commented-export.txt
python json-syntax-gate.py reviewed-export.json
python reviewed-json-to-csv.py reviewed-export.json new-output.csv
The first command intentionally exits with status 1 for the supplied invalid fixture. The reviewed copy exits with status 0. Place both Python files in the same directory. The exporter requires a nonempty array with exactly the string fields id, url and note, and creates a new CSV rather than overwriting an existing file.
What the local execution established
On 2026-10-07, Python 3.12.14 rejected the unchanged fixture at line 2, column 3. The reviewed copy parsed successfully and exported 2 records and 3 columns. Parsing that CSV back showed that every string cell matched the reviewed JSON. Both URLs, the literal comment-like text, the escaped quotation and ,] survived. The evidence download includes input hashes, error details and output cells.
These measurements apply only to this original fixture and these scripts. A hash identifies the bytes checked; it does not certify that a repair expresses the source author's intent. Here, the intended records are explicitly shown in the reviewed copy. For a real export with uncertain intent, ask its producer rather than inventing a missing value.
Keep the gate's limits visible
This gate uses Python's decoder, rejects duplicate names and nonstandard numeric constants, and retains numeric token text rather than forcing it through a float. It is not a full schema or Unicode interoperability validator. Accepted syntax does not determine table shape, field meaning or whether an ID is safe to open in a spreadsheet.
The examples read small UTF-8 inputs into memory and cap each input at 2,000,000 bytes as a deliberate recipe policy. They do not stream huge files or fix encodings. The exporter supports only the declared three-string-field shape; it rejects numeric or nested fields. Review untrusted spreadsheet content separately.
After syntax is resolved, check repeated object keys and verify every exported row and field. For number spelling, retain the original decimal token before float parsing.