ComfyUI Node

Random JSON Value

Pull a random value out of a nested JSON structure

By Q-Bug4·Created 2 years ago·Updated about a year ago· 11
Random JSON Value
    • random_value
    json_input
    max_depth-1

    Wildcard files are the usual way people randomize prompt fragments in ComfyUI - a flat text file, one option per line. Random JSON Value covers a different case: your options aren't flat, they're organized into a nested structure - categories of styles, grouped presets, whatever - and you want to grab something at random from anywhere inside that structure without caring exactly how deep it sits.

    How it works

    The README's own example is the clearest way to picture it: a JSON like {"colors": ["red", "blue", "green"], "sizes": {"S": 10, "M": 20, "L": 30}}. Point this node at that structure and it can return any value from anywhere inside it - a color, or a size's numeric value - treating the whole nested thing as one pool to draw from, rather than making you pick a specific sub-path first.

    The inputs and outputs that matter

    • json_input - multiline string, the JSON structure you're drawing a random value from. Works with nested objects, arrays, or a mix of both.
    • max_depth - INT, default -1, range -1 to 100. This is the one setting that actually shapes behavior: -1 means no depth limit, so the node can reach all the way down into however deeply nested your structure is. Set it to a smaller positive number if you want to bias selection toward shallower entries - useful if your JSON mixes a few top-level simple values with deeply nested categories, and you don't want the deep stuff to dominate every draw.
    • random_value (output) - whatever got picked, as a STRING, ready to wire straight into a prompt or wherever else a random pick belongs.

    Genuinely one input you'll tune (max_depth), one you provide (json_input), one thing comes out.

    How to install it

    Search ComfyUI Manager for "Simple JSON Parser" or "Json Node"; if it's not listed, install it manually:

    cd ComfyUI/custom_nodes
    git clone https://github.com/Q-Bug4/Comfyui-Simple-Json-Node
    

    Restart. No dependencies, no models to download - string/dict manipulation only, so nothing heavy here regardless of which node in the pack you're installing for.

    Common issues & troubleshooting

    Same value keeps coming up. This node has no seed input documented in the schema, so whatever randomness ComfyUI's own caching does around unchanged inputs may apply - if json_input and max_depth don't change between runs and the node's outputs get cached rather than re-executed, you can end up seeing the same pick repeatedly. If you need a fresh pick every queue, make sure something in the graph is actually forcing re-execution (the usual trick in ComfyUI is having at least one changing input upstream, like a seed, feeding into the chain).

    Picks feel skewed toward one part of the JSON. This is what max_depth is for - if your structure has a shallow flat list next to a deeply nested category tree, an unrestricted draw (max_depth=-1) can end up weighted toward whichever side of the structure has more total leaf values, simply because there are more of them to land on. If your draws feel lopsided, try capping max_depth to keep the selection closer to the top level, or restructure the JSON so the pools you want equal weight for are roughly the same size.

    Invalid JSON in, error out. Same as everywhere else in this pack - malformed json_input raises a ValueError rather than falling back to some default. If this node is fed from a file or another node's output rather than a literal you typed, validate the JSON is well-formed upstream (a quick pass through JSONParserNode with an empty path is a cheap sanity check) before wiring it into this one.

    Wanted a random pick from one specific branch, not the whole tree. If you only want randomness within, say, the colors list and not across the whole document, extract that sub-structure first with JSONParserNode and a path, then feed that narrower JSON into RandomJSONValueNode - don't rely on max_depth alone to scope the draw, since it limits depth, not which branch gets searched.

    Categoryutils

    Inputs (2)

    NameTypeDefaultDescription
    json_inputSTRING
    max_depthINT-1-1–100

    Outputs (1)

    NameTypeDescription
    random_valueSTRING