ComfyUI Node

JSON Iterator

Batch your workflow over a stack of JSON files

By ComfyUI-Kelin·Created 2 years ago·Updated 4 months ago· 27
JSON Iterator
    • json_string
    • current_index
    • total_items
    â—„file_pathâ–ş
    â—„start_index0â–ş
    â—„auto_nexttrueâ–ş
    â—„reset_iteratorfalseâ–ş

    When "run this workflow once" isn't enough, let it run itself

    ComfyUI workflows are single-shot by nature: queue it, it runs, it stops. JSON Iterator is the pack's batch tool - a node that feeds a different JSON item into your graph on every execution, walking through a folder of JSON files or the items of a JSON array one at a time. Hit Queue, let it chew through the whole stack, and when it's exhausted it stops itself. If you've got fifty caption files, a directory of character sheets, or an array of prompt objects you want to render one by one, this is the node that turns a workflow into a batch job without writing a loop.

    The mechanism is worth understanding because it's a bit unusual. The node keeps an in-memory counter per file path (a global dict in the pack's json_iterator module) and uses ComfyUI's IS_CHANGED hook to return a constant "this node changed" signal - so the node re-runs every execution and the counter advances. That's how a stateless graph gets state: the counter lives in the node's Python process, not in the workflow JSON.

    What it does with your input

    The single required input, file_path, points at either:

    • A directory - every .json file in it, sorted by filename, is iterated.
    • A file containing a JSON array - each array item becomes one iteration.
    • A file containing a JSON object - one iteration, the object itself.

    Each run, json_string outputs the current item's content, and you also get current_index and total_items as INT outputs so downstream nodes can track progress. The other inputs: start_index (where to begin, default 0), auto_next (on by default - advance automatically each run; turn it off and it always outputs start_index), and reset_iterator (set to true on one run to force the counter back to start_index).

    The stop behavior - read this

    When the iterator passes the last item it doesn't just return a value: it raises an interrupt that stops the ComfyUI queue ([JSONIterator] All items iterated. Stopping workflow.). That's deliberate - it's how the batch knows it's done - but it means an exhausted iterator looks like an interrupted run. If you're looping this inside a larger queue, plan for that: it halts the whole queue, not just your loop. For a simple "run over these files, stop when done" batch, it's exactly the behavior you want.

    Installing

    It's one of twelve nodes in 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
    

    Found under 🚦ComfyUI_LLMs_Toolkit/JSON. Dependencies: Pillow and aiohttp only.

    Gotchas

    The big one is the state lives in the process thing. The counter is a module-level global keyed by file path, so restarting ComfyUI resets it - and if two nodes point at the same path, they share one counter. reset_iterator exists precisely for that: flip it true, run once, flip it back. Second: this reads local files by path - there's no upload widget, so you'll want a folder where ComfyUI (and the OS user running it) can actually read. And third, remember it stops the queue when exhausted, which will surprise you the first time your whole batch "dies" at the end - it didn't, it finished.

    Category🚦ComfyUI_LLMs_Toolkit/JSON

    Inputs (4)

    NameTypeDefaultDescription
    file_pathSTRING—
    start_indexINT00–18446744073709550000—
    auto_nextBOOLEANtrue—
    reset_iteratorBOOLEANfalse—

    Outputs (3)

    NameTypeDescription
    json_stringSTRING—
    current_indexINT—
    total_itemsINT—