Nodes/Basic data handling/create DICT from JSON string
ComfyUI Node

create DICT from JSON string

Stop regex-ing fields out of JSON by hand

By StableLlama·Created about a year ago·Updated 3 days ago· 49
create DICT from JSON string
    • dict
    json_string{}

    JSON is how everything that isn't a tensor talks to ComfyUI. A local LLM hands you {"prompt": "...", "cfg": 4.5}. An API returns a pile of keys. Your saved parameter preset is a JSON file on disk. ComfyUI itself has no dictionary type, so the usual outcome is someone chaining three string nodes and a prayer to pull one field out. This node is the one-line Python function that ends that: give it a JSON object as a string, get a real DICT you can query.

    What it does under the hood

    It calls json.loads and then checks the result. If the string isn't valid JSON you get a ValueError reading Invalid JSON in input string: <details> - the actual decoder complaint, which is genuinely useful for finding your typo. If it parses but isn't a dictionary, you get The JSON string must be a JSON object (enclosed in curly braces), not <type>. So arrays bounce, numbers bounce, bare strings bounce. Objects only.

    There's no cleverness beyond that, and that's the point. It's the entry point to the pack's whole DICT family - around thirty nodes for get, get_multiple, keys, values, items, contains_key, length, merge, update, invert, filter_by_keys, exclude_keys and so on. Parse once, then interrogate the thing like a programmer instead of a string-wrangler.

    Inputs and output

    json_string (required, STRING) is the only input - a text widget defaulting to {}, with the author's description right there in the tooltip. It's a normal STRING socket, so you can wire an upstream node's text output into it just as happily as you can type into it. That's the whole flow: LLM node → this → fields.

    The single output, dict, is the DICT the pack parsed - one value on one wire, not a per-item list.

    Then wire it into get and you have a field. That node takes the dict plus a key, and it has an optional default socket where you put your fallback, which is the difference between a missing key quietly becoming None and a missing key becoming a sane value. keys and length are the other two I'd reach for first - if you're parsing model output, always look at what you actually got before you trust a key to be there.

    The payoff workflow: one prompt text box where the LLM emits {"prompt": "...", "negative": "...", "steps": 28, "cfg": 4.5}, parsed once, each field fanned out to the sampler and the filename. That's five widgets and five wires collapsed into one, and - more importantly - a structured answer can't leak chat prose into your conditioning the way free text can.

    The failure everyone hits

    Your LLM says Here is the JSON you asked for: and then a fenced code block. That's a hard decode error here; there's no salvage mode, and there shouldn't be, because guessing at broken JSON is worse than failing. The KB's account of LLM nodes in the graph is blunt about this being a signature failure mode of the whole category - a model that isn't constrained hands you its conversational habits as literal output. Strip it upstream before it reaches this node: the pack's own strip, replace and removeprefix string nodes will do it, or better, constrain the model.

    Other honest gotchas:

    • It's JSON, not Python. Single quotes, True/False/None, trailing commas and comments all fail. Keys must be double-quoted.
    • You pasted an array. The error message names the type outright and tells you to reach for the LIST node instead - which is the right call, not a workaround.
    • Nested objects are fine and come back as nested DICTs. But when a model escapes JSON inside a JSON string, you get a STRING and need a second instance of this node to parse it.
    • Duplicate keys: last one wins, silently. JSON says that's correct behavior; your LLM occasionally says something else.

    Installing it

    It's part of Basic data handling by StableLlama - GPL-3.0, no runtime dependencies at all (the pyproject.toml dependency list is empty), no model downloads. Manager route: search "Basic data handling", install, restart ComfyUI. Manual route:

    cd ComfyUI/custom_nodes
    git clone https://github.com/StableLlama/ComfyUI-basic_data_handling
    # restart ComfyUI
    

    The node lives under Basic/DICT in the add-node menu; its display name is the lowercase phrase create DICT from JSON string, so search the pack name if you're hunting. It was added in release 2.0.0 (September 2026) alongside create LIST from JSON string and continue if not empty - if it's missing, update the pack.

    One structural thing to expect: DICT is a custom type this pack defines, so the output only wires into nodes that declare it. Other packs' JSON utilities won't accept that socket, and that's fine - the pack has enough dict nodes that you rarely need to leave.

    CategoryBasic/DICT

    Inputs (1)

    NameTypeDefaultDescription
    json_stringSTRING{}STRING containing a JSON object to parse, e.g. {"a": 1}.

    Outputs (1)

    NameTypeDescription
    dictDICTThe DICT parsed from the JSON object string.