Nodes/Comfyui_PDuse/PD_CaptionJSON编辑器
ComfyUI Node

PD_CaptionJSON编辑器

Batch-edit captions stored as JSON

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD_CaptionJSON编辑器
    • json_string
    • caption
    • lang
    json_string
    new_caption
    add_prefix
    add_suffix
    replace_from
    replace_to
    new_langen

    PD_CaptionJSON编辑器 (PD_CaptionJSONEditor) parses a JSON string holding a caption, lets you edit it - replace wholesale, add a prefix or suffix, find-and-replace words - and outputs the edited JSON plus the extracted caption and language. It's built for the growing number of captioning pipelines that store each caption as JSON with caption and lang fields instead of a bare text file.

    How it works

    You feed it a JSON string like {"caption": "a red car", "lang": "en"}, and it picks out the caption and lang fields. Then it applies your edits in a defined order: new_caption completely replaces the caption if set, otherwise add_prefix/add_suffix wrap it, and replace_from/replace_to do find-and-replace. The replace fields are multi-line: each line is a search term, paired positionally with a line in replace_to, and if the replace_to list runs short, the leftover terms get replaced with empty strings (i.e., deleted). new_lang sets the language field. Every edit is reflected back in the JSON output, and you also get caption and lang as separate outputs for wiring directly into a text encoder.

    The batch story is where this gets interesting: it accepts a list of JSON strings as input and emits lists for all three outputs. That slots straight into the pack's "process a folder of captions" workflows - load a directory of JSON caption files, edit them all in one run, save them back out.

    The inputs that matter

    • json_string - the JSON to edit (required). Multiline, and force-wireable.
    • new_caption - if non-empty, completely replaces the caption.
    • add_prefix / add_suffix - text wrapped around the existing caption.
    • replace_from / replace_to - paired find/replace lists.
    • new_lang - overwrites the language field (default en).

    Outputs: json_string, caption, lang - all lists when fed a list.

    Installing

    Part of the 7BEII/Comfyui_PDuse pack. ComfyUI Manager → search "Comfyui_PDuse" → install, or:

    cd ComfyUI/custom_nodes
    git clone https://github.com/7BEII/Comfyui_PDuse
    cd Comfyui_PDuse
    pip install -r requirements.txt
    

    Restart after. No models; plain Python JSON handling, so no special dependencies beyond what the pack already installs.

    Gotchas

    If the input isn't valid JSON, the node doesn't fail hard - it falls back to treating the whole string as the caption with lang set to en, which is forgiving but means malformed input can silently pass through with edits applied to a misread caption. Also, "replace" is order-dependent and positional: a term on line 3 of replace_from is replaced by line 3 of replace_to, so keep the lists aligned. If your caption files are plain .txt rather than JSON, the companion PD_CaptionJSONParser is the read side - this node is the write side.

    CategoryPDuse/Text

    Inputs (7)

    NameTypeDefaultDescription
    json_stringSTRING
    new_captionoptSTRING
    add_prefixoptSTRING
    add_suffixoptSTRING
    replace_fromoptSTRING
    replace_tooptSTRING
    new_langoptSTRINGen

    Outputs (3)

    NameTypeDescription
    json_stringSTRING
    captionSTRING
    langSTRING