Nodes/ComfyUI/Extract Text from JSON
ComfyUI Node Runs on cloud

Extract Text from JSON

Pull one value out of a pile of braces

By Comfy-Org·Created 4 years ago·Updated 21 days ago· 121,575
Extract Text from JSON
    • STRING
    json_string
    key

    You've got a blob of JSON sitting in a text field and you need one value out of it. Extract Text from JSON (JsonExtractString) is that tool: give it a JSON string and a key name, and it returns the matching value as plain text. It's one of those plumbing nodes that looks trivial until the moment you need it, and then it's the thing that unblocks your whole graph.

    Where does JSON show up in a ComfyUI workflow? Most commonly from LLM nodes: ask Generate Text to "return JSON," and it will - happily, with no prompt discipline at all. Or from an API call node that returns a response object, or from a workflow that reads a .json file and hands you the raw string. In all those cases the useful data is buried one level in: the description field, the url, the seed. This node digs it out.

    How it works

    The mechanism is short: parse the input with json.loads, check if the result is a dict containing your key, and if so convert the value to a string with str(). Two behaviors worth knowing because they're easy to trip on:

    • It only reads top-level keys. If your JSON is {"result": {"caption": "hello"}} and you ask for caption, you get nothing. There's no dot-path support here - no result.caption. To reach nested values you extract result first, then run the node again on that output. Chaining is the intended design.
    • It returns "" on any failure. Missing key, malformed JSON, wrong type - all quietly produce an empty string rather than an error. That's a feature once you know it: it gives you graceful fallbacks. A downstream node that sees empty text just sees empty text; the graph doesn't die.

    One more subtlety: the value is stringified with str(). Numbers and booleans come back as text ("42", "true"), and a null value returns empty string rather than the string "null". If you need the number out of JSON, you're in the wrong node - this one is explicitly for text, and the output is always a STRING.

    The inputs that matter

    • json_string - the JSON text. Multiline, so paste or wire in the whole blob.
    • key - the top-level key you want, exactly as written including case: "caption" won't match "Caption". Single-line field; keys with weird characters are valid JSON but you type them verbatim.

    Output: a single STRING with the extracted value (or "" when there's nothing to extract). Wire it into a prompt, a filename, a condition - anywhere text goes.

    Common issues

    The gotcha people actually hit is expecting a nested lookup or a path. If you wrote data.items[0].name and got nothing, that's why - pull the top-level key first, then drill down with a second node. Second gotcha: the output is always a string, so a value like 3.5 can't feed a float input without a conversion node. And third, if the JSON itself is invalid (truncated, double-encoded, or a file that's actually a list of objects), you'll silently get "" and spend a while wondering why - throw the string at a JSON validator if in doubt. For the reverse direction - turning a dict back into text - there's Convert Dictionary to String, and together they make a clean round trip.

    Categorytext

    Inputs (2)

    NameTypeDefaultDescription
    json_stringSTRING
    keySTRING

    Outputs (1)

    NameTypeDescription
    STRINGSTRING