data parsing ai_generated true

json.decoder.JSONDecodeError: Expecting property name enclosed in double quotes at trailing comma

ID: data/json-trailing-comma-parse-error

Also available as: JSON · Markdown
92%Fix Rate
95%Confidence
3Evidence
2023-01-01First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
any active

Root Cause

Trailing commas are valid in JavaScript but invalid in JSON (RFC 8259). Many developers write JSON by hand or copy from JS and include trailing commas. JSON.parse and json.loads both reject them.

generic

Workarounds

  1. 90% success Use json5 library for human-written config files that need comments/trailing commas
    import json5; data = json5.load(open('config.json5'))  # allows trailing commas, comments
  2. 82% success Strip trailing commas with regex before parsing (for trusted input only)
    import re; clean = re.sub(r',\s*([}\]])', r'\1', json_str); data = json.loads(clean)
  3. 88% success Use a linter (jsonlint) to validate JSON before processing
    npx jsonlint file.json  # or: python -m json.tool file.json

Dead Ends

Common approaches that don't work:

  1. Allow trailing commas because JSON is 'just JavaScript object notation' 95% fail

    JSON is NOT JavaScript. JSON is a strict subset. Trailing commas, single quotes, unquoted keys, and comments are all invalid JSON.

  2. Use eval() or Function() to parse JSON with trailing commas 98% fail

    eval() is a critical security vulnerability for untrusted data. Never use eval() to parse JSON.