Nodes/ComfyUI-text-replace/Extract JSON Node
ComfyUI Node

Extract JSON Node

Pull a { ... } object out of a wall of text and pretty-print it

By banqingyuan·Created 2 years ago·Updated 2 years ago· 0
Extract JSON Node
    • STRING
    input_stringSome text {"key": "value"} more text

    Every now and then a node pack throws in a plain utility that has nothing to do with its theme, and this is it. Extract JSON Node is a tiny text tool in a pack that's otherwise about erasing and redrawing text on images. It takes one string, finds the first { ... } object in it, validates that it parses as JSON, and returns it formatted with two-space indentation. That's the entire feature set.

    It's genuinely handy in one specific situation: when some other node or an LLM hands you a string with a JSON object buried inside prose or log noise, and you want a clean, pretty-printed copy to inspect or pipe downstream. Think of it as a regex-based extractor with a JSON validator bolted on.

    How it works

    The whole logic is a regex plus json.loads. It searches the input for the pattern \{[^{}]*\} - a brace-delimited block containing no nested braces - grabs the first match, tries to parse it, and re-serializes it with indent=2. If nothing matches or the parse fails, it returns an empty string and prints a short parse error to the console.

    Input and output

    One input, input_string (multiline, default Some text {"key": "value"} more text - a decent illustration of the use case). One output, STRING: the formatted JSON, or "" on failure.

    The limits that will bite you

    • It only matches { ... } objects. Arrays like [1, 2, 3] don't match the regex, so you can't use this to fish an array out of text. That matters for this very pack: OCRLocNode's merged_rectangles output is a JSON array, so this node can't extract it.
    • No nested braces. The pattern explicitly excludes them. A JSON object containing another object won't match cleanly - you'll get the innermost fragment at best.
    • It returns an empty string, not an error. Wire the output straight into a node that needs real JSON and you'll get a confusing failure downstream. If your pipeline breaks mysteriously, check whether this returned "".
    • First match only. Multiple objects in one string? You get the first one.

    Install

    Same as the rest of the pack - ComfyUI Manager, search ComfyUI-text-replace, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/banqingyuan/ComfyUI-text-replace
    

    Restart, and it appears under Utils. No dependencies beyond Python's standard json and re modules, so this node loads even if the pack's heavier install steps went sideways.

    It's a one-job node and it's fine at that job. The author could have made it more general - extract arrays, handle nested objects, return the raw match instead of a re-serialized copy - but as a cleanup step between an LLM's prose and a JSON-consuming node, it saves you from writing a regex in a text field. Just keep its three limitations in mind.

    CategoryUtils

    Inputs (1)

    NameTypeDefaultDescription
    input_stringSTRINGSome text {"key": "value"} more text

    Outputs (1)

    NameTypeDescription
    STRINGSTRING