PageIndex icon indicating copy to clipboard operation
PageIndex copied to clipboard

JSON recovery rewrites quoted "None" and whitespace inside string values

Open llz1515 opened this issue 4 weeks ago • 1 comments

Problem

JSON recovery changes user-visible string content: Nonequilibrium Methods becomes nullquilibrium Methods, and quoted None becomes null. The important remaining case for #382 is a valid quoted string alongside an unquoted Python-style None: its strict-first path cannot bypass recovery for that mixed input.

Baseline and environment

Reproduced on current main at 646deeb650005885f44001471d15ab9eab1bbc13 (rechecked 2026-11-04), Python 3.11.15, macOS 26.6 arm64. Minimal reproduction dependencies: requests 2.34.2, urllib3 2.8.0, PyPDF2 3.0.1, PyYAML 6.0.3 and python-dotenv 1.2.4. Run from the repository root with PYTHON_DOTENV_DISABLED=1 PYTHONPATH=. python repro.py.

No model API or live PageIndex service was called. These are deterministic function/transport reproductions, not real-model end-to-end tests. No issue template or contribution guide was present in the repository at this baseline.

Minimal reproduction

import ast
import json
import logging
import re
import subprocess
from pageindex.utils import extract_json

samples = [
    '{"title": "Nonequilibrium  Methods", "note": "None"}',
    '{"title": "Nonequilibrium  Methods", "note": "None", "page": None,}',
    '{"title": "literal ,} and ,]", "page": None,}',
]
for sample in samples:
    print("main:", repr(sample), "->", extract_json(sample))

# Execute only the three parser helpers from the reviewed PR, without its
# historical module initialization or any model/provider dependency.
source = subprocess.check_output(["git", "show", "review/pr382:pageindex/utils.py"], text=True)
module = ast.parse(source)
module.body = [n for n in module.body if isinstance(n, ast.FunctionDef) and
               n.name in ("get_json_content", "_decode_embedded_json", "extract_json")]
namespace = {"json": json, "re": re, "_logger": logging.getLogger("repro382")}
exec(compile(module, "PR382-parser-helpers", "exec"), namespace)
for sample in samples:
    print("PR382:", repr(sample), "->", namespace["extract_json"](sample))

Actual result

main: '{"title": "Nonequilibrium  Methods", "note": "None"}' -> {'title': 'nullquilibrium Methods', 'note': 'null'}
main: '{"title": "Nonequilibrium  Methods", "note": "None", "page": None,}' -> {'title': 'nullquilibrium Methods', 'note': 'null', 'page': None}
main: '{"title": "literal ,} and ,]", "page": None,}' -> {'title': 'literal } and ]', 'page': None}
PR382: '{"title": "Nonequilibrium  Methods", "note": "None"}' -> {'title': 'Nonequilibrium  Methods', 'note': 'None'}
PR382: '{"title": "Nonequilibrium  Methods", "note": "None", "page": None,}' -> {'title': 'nullquilibrium  Methods', 'note': 'null', 'page': None}
PR382: '{"title": "literal ,} and ,]", "page": None,}' -> {'title': 'literal } and ]', 'page': None}

Expected behavior and scope

Valid JSON should parse without string rewrites. When supported recovery converts unquoted None or removes trailing commas, quoted strings must retain spaces, escaped quotes, backslashes, Unicode and punctuation such as ,}/,]. This issue is limited to preserving quoted content during recovery; it does not request an entire parsing framework rewrite or an eval fallback.

Cause and related work

Current main replaces None globally and collapses whitespace before json.loads, then replaces ,] and ,} globally. #382 (head 5012e1ec416d214df5ef6861ef4867ebec13a197) correctly preserves the legal JSON example via strict decoding, but its recovery still replaces quoted None and comma/bracket sequences. The reproduction runs its three parser helpers directly from the fetched PR source with stdlib dependencies only; fetch with git fetch origin refs/pull/382/head:refs/remotes/review/pr382 first. This complements #382/#326 and differs from broader schema/framework work in #99/#339/#195. Historical #129 also mentions quoted None corruption; the current-main and mixed-input evidence above establish the remaining behavior. Open/recently closed items and extract_json issue/PR searches were rechecked before filing.

Source: current extraction, related PR #382

AI-assisted investigation; reproduction results above were executed locally.

llz1515 avatar Nov 04 '26 04:11 llz1515

I have a fix ready for this: strict parse first, then repair None and trailing commas only outside quoted strings, with regression tests. It doesn't touch #382's prose handling. I'll open a PR unless you're already working on it.

MeteHanYilmaz0 avatar Nov 05 '26 13:11 MeteHanYilmaz0