CheckMySchema

Structured data explained

Can you have a trailing comma in JSON?

No. Trailing commas are not allowed in JSON. Here’s why, what errors you’ll see, and how to find and fix them fast without breaking portability.

    Minimal editorial illustration full frame showing two side by side JSON code panels on a midnight background one panel with a highlighted trailing comma error and the other corrected

    No. JSON does not permit trailing commas in arrays or objects. The JSON grammar defines arrays as values separated by commas and objects as members separated by commas, with no comma after the final item. Strict parsers must reject inputs that don’t match this grammar, so a trailing comma makes the JSON invalid. See the specification in RFC 8259 for arrays and objects, and parser requirements. RFC 8259: JSON

    Why JSON forbids trailing commas

    JSON’s formal grammar allows commas only between elements, not after the last one. In other words:

    • Object: member, member, … but not a comma after the last member
    • Array: value, value, … but not a comma after the last value

    That’s why a closing ] or } must directly follow the final element or member. RFC 8259: JSON

    It’s also why JSON is stricter than JavaScript. JavaScript source code allows trailing commas in many places, but JSON explicitly does not. MDN calls this out and shows that trailing commas in JSON are invalid. MDN: Trailing commas

    Minimal editorial diagram of two code editor panels on a midnight background showing a JSON parse error caused by a trailing comma and a corrected version after removal

    What errors you’ll see

    Passing JSON with a trailing comma to a strict parser typically raises a parsing error. For example, in JavaScript, JSON.parse throws a SyntaxError if a trailing comma is present, as MDN demonstrates. MDN: Trailing commas

    Typical symptoms include messages like “unexpected ]” or “unexpected }” at the position of the dangling comma.

    How to detect and fix trailing commas

    Practical steps

    • Reproduce with a strict parser (for example, run your JSON through a parser that rejects extensions). The JSON spec allows parsers to accept extensions, but only strict parsing guarantees portability. RFC 8259: Parsers
    • Search for patterns like ,] or ,} to find likely trailing commas.
    • Remove the final comma so the closing bracket or brace immediately follows the last element or member.
    • Re-validate in all target environments to confirm the fix. Some tools accept non-standard forms, so cross-checking is important. RFC 8259: JSON
    Minimal 2D vector three step diagram showing parse with strict validator remove final comma and re validate across consumers on a midnight background

    Permissive modes exist, but are non-standard

    Some libraries offer an opt-in mode to accept trailing commas, which can be convenient for hand-edited data. For example, the JSON for Modern C++ library provides an ignore_trailing_commas parse option that treats trailing commas as acceptable when enabled. This is explicitly non-default and non-standard. nlohmann/json: trailing commas

    Vendors may also choose to accept and strip trailing commas to simplify certain authoring workflows. TeamDesk documents doing this server-side, but notes it as a product choice, not standard JSON. Files relying on such behavior can still fail in strict tools. TeamDesk: trailing commas announcement

    Remember: RFC 8259 says parsers may accept extensions, but generators must produce strictly conforming JSON. If you don’t control every consumer, avoid trailing commas. RFC 8259: Parsers and Generators

    Related reading on CheckMySchema

    Quick fix checklist for trailing commas in JSON

    FAQ

    Is a trailing comma ever safe in JSON?

    Only if every consumer uses a permissive mode or tool that strips or ignores it. That’s non-standard and risks breakage elsewhere. RFC 8259: JSON nlohmann/json TeamDesk

    Why does JavaScript allow trailing commas but JSON doesn’t?

    They are different languages: JavaScript permits trailing commas in many syntactic positions, but JSON disallows them. MDN: Trailing commas

    CheckMySchema logo

    Related guides

    Official sources and further reading