Skip to content

[Bug]: A string value starting with a leading unescaped quote (""content) silently drops the escaped quote #220

Closed

Description

@henryjhj

Version of the library

0.63.3

Describe the bug

When a JSON string value begins with a literal, unescaped double quote
(i.e. the raw text is "key": ""content..., meaning: opening quote, then
immediately another quote that is meant to be a literal character, then
the rest of the content), repair_json treats the second quote as if the
string were empty, and silently drops it instead of escaping it.

This looks related to #46 (unescaped quote followed later by a comma), but
that issue is about a quote appearing in the middle of a string and is
now fixed in this version. This report is about the quote being the very
first character of the content, which still fails.

How to reproduce

>>> from json_repair import repair_json
>>> repair_json('{"title": ""hello" world"}')
'{"title": "hello\\" world\\""}'

Note the leading \" before hello is missing entirely, and the trailing
quote is duplicated/misplaced as a result. Compare with a control case
where the quote is not at the very start of the string, which repairs
correctly:

>>> repair_json('{"title": "hello \\"world\\" test"}')
'{"title": "hello \\"world\\" test"}'   # correct, quote is not at the start

The bug reproduces regardless of what follows (with or without a trailing
comma, with or without other keys):

>>> repair_json('{"title": ""hello" world", "b": "y"}')
'{"title": "hello\\" world", "b": "y"}'

>>> repair_json('{"a": "x", "title": ""hello" world"}')
'{"a": "x", "title": "hello\\" world\\""}'

This is common in real-world LLM output whenever a generated sentence
begins with a quoted phrase, e.g. a news headline like:

"title": ""We love our Americans": German town..."

Expected behavior

The leading quote should be treated as a literal character and escaped,
the same way a quote in the middle of the string is already handled:

>>> repair_json('{"title": ""hello" world"}')
'{"title": "\\"hello\\" world"}'

>>> repair_json('{"title": ""hello" world", "b": "y"}')
'{"title": "\\"hello\\" world", "b": "y"}'

Activity

  1. henryjhj commented on Aug 24, 2026

    @henryjhj
    Author

    Suggested fix direction

    In parse_string(), when the parser is at the very start of a string's
    content (position right after the opening quote) and immediately sees
    another ", it currently seems to short-circuit to "empty string" instead
    of doing the same look-ahead-for-terminator check it already does for
    quotes found later in the string. Applying the same "peek ahead for a
    plausible terminator (,, }, ], :, or end of input)" logic at
    position 0 of the string content — instead of special-casing it as
    automatically empty — should resolve this without affecting the already-
    fixed #46 case.

  2. mangiucugna commented on Aug 25, 2026

    @mangiucugna
    Owner

    Hi thanks for reporting, agree this is problematic so I implemented a fix that will fix ""hello" world" to '"hello" world' and ""hello"} to "hello"}

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions