ComfyUI Node

Extract Prompt from JSON

Dig a Prompt Out of a Workflow JSON Without Knowing Exactly Where It Lives

By Tlant·Created 2 years ago·Updated 5 months ago· 2
Extract Prompt from JSON
    • prompt_text
    • matched_path
    • error
    workflow_json
    json_path
    auto_selectfalse

    You've got a workflow JSON - maybe pulled out of a PNG with the pack's ExtractJsonFromPngTlant, maybe pasted from a shared file - and buried inside it is the prompt that made the image. That prompt isn't sitting at the top level; it's nested three levels deep under something like 293.inputs.text_0. This node finds it for you. Its party trick is auto_select: flip it on and it just hunts down the longest text string in the whole JSON and hands it back, which in a ComfyUI workflow is almost always your positive prompt.

    How it works

    It parses the workflow_json string, then picks one of two paths. With auto_select on, it recursively walks the whole structure and returns the single longest string field (anything shorter than 20 characters is ignored - that filters out node titles and stray labels). With auto_select off, it walks the dot-notation path you give it in json_path, like 293.inputs.text_0 or inputs[0].text. The path parser handles both dotted keys and [index] bracket notation.

    The error output is unusually helpful. Wrong path? It tells you the top-level keys it did find and whether the first segment of your path exists. Non-string value at the target? It serializes it and warns you about the type instead of failing silently.

    Inputs and outputs

    • workflow_json - the JSON as a string (multiline; you can paste it or wire it in from ExtractJsonFromPngTlant).
    • json_path - dot-notation path, only used in manual mode.
    • auto_select - boolean; on = longest-text scan, off = follow json_path.

    Outputs: prompt_text (the found text), matched_path (where it was found - super useful for learning the path so you can switch to manual mode later), and error (empty on success).

    Install

    It's a tlant/utils node in the Tlant pack:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Tlant/ComfyUI-OllamaPromptsGeneratorTlant
    

    restart ComfyUI, or search "Extract Prompt from JSON" in ComfyUI Manager.

    Where people get tripped up

    • "Longest string" is a heuristic, not a guarantee. For a normal ComfyUI workflow it reliably lands on the positive prompt because that's the longest text in the graph. But if you've got a giant multi-line negative prompt or a big style-block text node, the "longest" might be that instead. Check matched_path; if it's wrong, switch to manual mode.
    • You still need valid JSON. Paste in a raw API response or a workflow field that's been truncated and you'll get a parse error - the node is honest about it, but there's no recovery.
    • The 20-character floor. Short prompts (a single tag, a name) fall under the filter in auto mode and won't be found. Use manual mode for those.

    It's the missing "unpack the recipe" step that turns ExtractJsonFromPngTlant from a curiosity into a pipeline: PNG in, positive prompt out, feeding img2img or re-generation automatically.

    Categorytlant/utils

    Inputs (3)

    NameTypeDefaultDescription
    workflow_jsonSTRING
    json_pathSTRING
    auto_selectBOOLEANfalse

    Outputs (3)

    NameTypeDescription
    prompt_textSTRING
    matched_pathSTRING
    errorSTRING