ComfyUI Node

JSON Fixer

For when the LLM gives you almost-JSON

By ComfyUI-Kelin·Created 2 years ago·Updated 4 months ago· 27
JSON Fixer
    • fixed_json
    ◄text—►

    The truth about LLMs and JSON: they get it almost right

    Every LLM-based workflow eventually collides with this: you ask the model for structured output, it agrees, and then it hands you JSON with trailing commas, single quotes instead of double, keys that never got quoted, or - the classic - a perfectly good JSON object wrapped in a ```json markdown fence with a sentence of commentary on top. JSON Fixer is the pack's shrug-and-recover node: throw that mess in, get clean JSON out.

    It's the piece that makes the pack's JSON story coherent. The Builder family makes clean JSON, the Adapter gets the LLM to emit JSON (roughly), Extractor pulls values out - and Fixer sits between the LLM and everything else, absorbing the reality that models don't emit spec-perfect JSON. If you've ever parsed LLM output and watched your whole graph die on one stray comma, you know why this node exists.

    How it works

    It tries increasingly aggressive strategies in order: first a direct parse (already-valid JSON passes straight through, re-serialized), then extracting anything inside markdown code fences, then grabbing the first {...} or [...] block from surrounding prose, then applying the classic fixes - stripping trailing commas, converting single-quoted keys and values to double quotes. Last resort, it brute-force scans for the largest valid JSON substring, capped at the first 10,000 characters so the search doesn't go quadratic on big inputs. If nothing works, you get an "Unable to fix JSON" error string with the last parse error, rather than a crash.

    Inputs and output

    • text (required, connectable) - the raw, probably-malformed JSON. Wire an LLM adapter's response output straight in.
    • Output: fixed_json - clean, minified-ish JSON (re-serialized with ensure_ascii=False, so Chinese and other non-ASCII text stays readable).

    Installing

    Part of the HuangYuChuh/ComfyUI-LLMs-Toolkit pack. ComfyUI Manager → search ComfyUI-LLMs-Toolkit → Install → restart, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/HuangYuChuh/ComfyUI-LLMs-Toolkit
    cd ComfyUI-LLMs-Toolkit
    pip install -r requirements.txt
    

    Find it under 🚦ComfyUI_LLMs_Toolkit/JSON. Light dependencies (Pillow + aiohttp), no models.

    Gotchas

    Don't expect miracles from the regex fixes - they handle the common cases (quotes, trailing commas, fences, prose around JSON) and that's genuinely 90% of LLM output problems. What they don't handle: truncation mid-string (a JSON string that just cuts off at the token limit), genuinely hallucinated structure, or single-quoted values containing apostrophes. The reliable fix for truncation is asking the model for shorter output or raising max_tokens, not more regex. Also note the output is re-serialized, so formatting and key order are normalized - if a downstream node was relying on the LLM's original formatting, that's gone. Chain this into JSON Extractor (with a Builder or Combine upstream) and you've got a proper JSON pipeline.

    Category🚦ComfyUI_LLMs_Toolkit/JSON

    Inputs (1)

    NameTypeDefaultDescription
    textSTRING—

    Outputs (1)

    NameTypeDescription
    fixed_jsonSTRING—