Skip to content

[Bug]: A comment between an object key's colon and its value drops the value #240

Closed

Description

@michaelkillgta

Version of the library

0.64.2 (also master @ f13a96b)

Describe the bug

If a comment sits after a key's colon and before its value, the comment is parsed as the value. The real value is then lost, or it spills into a bogus key and flattens a nested object. Comments elsewhere (before a key, between array items, after a value) are already skipped correctly. LLMs often annotate JSON this way, for example "age": // estimated\n 42.

How to reproduce

from json_repair import repair_json
for s in ['{"a": /* comment */ 1}', '{"a": // note\n "x", "b": 2}', '{"a": /* c */ {"b": 1}}', '{"a": # c\n true}']:
    print(repr(s), "->", repair_json(s))

Output:

'{"a": /* comment */ 1}' -> {"a": ""}
'{"a": // note\n "x", "b": 2}' -> {"a": "", "x\", \"b": 2}
'{"a": /* c */ {"b": 1}}' -> {"a": "", "b": 1}
'{"a": # c\n true}' -> {"a": ""}

Expected behavior

The comment is skipped and the value is kept: {"a": 1}, {"a": "x", "b": 2}, {"a": {"b": 1}} and {"a": true}.

I have a small fix with tests ready (it skips comments in _parse_object_value before dispatching the value) and can open a PR if that works for you.

Activity

  1. michaelkillgta commented on Oct 26, 2026

    @michaelkillgta
    Author

    I have a fix ready, but this repo only accepts PRs from collaborators, so I couldn't open one. The branch is here if it's useful: https://github.com/michaelkillgta/json_repair/tree/fix/comment-before-object-value (commit 0fc3b33).

    Before parsing an object value, the parser now skips any #, // or /* */ comments and the whitespace after them. A lone / that doesn't start a comment is left alone. It adds test_parse_comment_between_colon_and_value to tests/test_parse_comment.py, which fails on master and passes with the fix. The full suite passes (420 passed, 2 skipped).

    Happy to adjust it or open a PR if you enable that for me.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions