ComfyUI Node

JSON Extractor

Pull one value out of an LLM's JSON blob

By ComfyUI-Kelin·Created 2 years ago·Updated 4 months ago· 27
JSON Extractor
    • value
    ◄json_string—►
    â—„keyâ–ş

    The node that turns an LLM's JSON answer into something you can use

    Ask an LLM for structured output and you get JSON - a whole blob of it. Then you stare at the blob and realize you need just the one field, say the prompt inside {"result": {"prompt": "..."}}, wired into your KSampler. JSON Extractor is the "give me this key" node: feed it a JSON string and a key name, and it returns the value. Simple on the surface, and the details are where it gets useful.

    The part that elevates it past a toy: nested keys via dot paths. Instead of writing your own parse chain, you type user.profile.name and it walks the object for you. That single feature makes it dramatically more useful than the usual "top-level key only" extractors you'll find in other packs, because real LLM output is always nested.

    How it works

    It parses the JSON, walks the dot-separated path you gave it, and returns the value. The genuinely thoughtful bit is type preservation: if the value is a string, integer, float, boolean, or null, it comes out as that native type on the output - so a numeric field can feed a node expecting a number, not a string that says "42". Dictionaries and lists get re-serialized to JSON strings, since there's no universal node type for them. If the key is missing or the path hits a non-object, you get a readable error string back (Key 'x' not found) instead of a crash.

    Inputs and output

    • json_string (required, connectable) - the JSON to dig into. Plug an LLM adapter's response or a JSON Builder's output straight in.
    • key (required) - the property name, with dot-path support for nesting.
    • Output: value - type *, meaning it adapts to whatever the extracted value is. Wire it anywhere a string or number is expected.

    Installing

    Ships in the HuangYuChuh/ComfyUI-LLMs-Toolkit pack. Install via ComfyUI Manager (search ComfyUI-LLMs-Toolkit), or:

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

    Restart and look under 🚦ComfyUI_LLMs_Toolkit/JSON. Only dependencies are Pillow and aiohttp.

    Gotchas

    Three things bite people. First, the input must be valid JSON - if you wire in raw LLM text that has markdown fences or trailing garbage around the JSON, extraction fails with an "Invalid JSON" message. That's what JSON Fixer in the same pack is for: run the messy output through it first, then extract. Second, remember the return type is polymorphic - if you're expecting a string and getting a number (or vice versa), that's type preservation doing its job, not a bug. Third, the error strings are returned through the output, so if you chain this straight into a text encoder you can accidentally encode "Key 'prompt' not found" as your prompt. When extraction is failure-critical, validate the output before it reaches the sampler.

    Category🚦ComfyUI_LLMs_Toolkit/JSON

    Inputs (2)

    NameTypeDefaultDescription
    json_stringSTRING—
    keySTRING—

    Outputs (1)

    NameTypeDescription
    value*—