Nodes/comfyui-easyapi-nodes/GetValueFromJsonObj
ComfyUI Node

GetValueFromJsonObj

GetValueFromJsonObj – ComfyUI Node

By lldacing·Created 3 years ago·Updated 8 months ago· 96
GetValueFromJsonObj
  • json
  • any
key
to_typedefault

What it is

GetValueFromJsonObj is the counterpart to ConvertToJsonStr: instead of turning data into a JSON string, it reaches into a JSON object (a dict, in Python terms) and pulls out one value by key. If you're consuming an API response inside a workflow, or you've got a node upstream that hands you a bundle of metadata as a single JSON blob and you only care about one field of it, this is how you get that field back out as a usable value rather than doing string parsing by hand.

It also does one thing plain dict-access wouldn't give you for free: type coercion on the way out, via the to_type option. Pull a field out and force it to a str, int, float, or bool regardless of how it was actually stored in the source JSON.

Inputs and outputs

  • json (type *, required) - the object to pull a value from. Typed as * rather than a strict JSON type so it accepts whatever upstream node handed you the data, dict-like or otherwise.
  • key (STRING, default empty) - the field name to look up.
  • to_type (enum, default default; choices: default, str, int, float, bool) - how to coerce the extracted value before it leaves the node. default means "return it as-is, whatever type it naturally is."

One output: any (type *) - the extracted (and possibly coerced) value.

How it works

Straightforward key lookup on the object you give it, then an optional cast based on to_type. The coercion is worth using deliberately rather than leaving on default whenever you know exactly what type the next node needs - API-sourced JSON is notoriously inconsistent about whether a number comes through as a string or a real number, and forcing the type here means you don't get bitten by that three nodes downstream where the error is much harder to trace back.

Installing it

Part of comfyui-easyapi-nodes. Via ComfyUI Manager: search "comfyui-easyapi-nodes", install, restart ComfyUI. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/lldacing/comfyui-easyapi-nodes.git
cd comfyui-easyapi-nodes
pip install -r requirements.txt

Nothing unusual for this node specifically, but the requirements install is worth doing regardless so the rest of the pack loads without import errors. git pull in the folder to upgrade.

Common issues

"I get an error, or an empty/None result, and I'm sure the key exists." Check the key spelling exactly - this is a literal key match, no fuzzy lookup - and check that json upstream is actually a dict-shaped object and not, say, a JSON string that still needs parsing first (if you're not sure, run it through this pack's ConvertToJsonStr and a debug/show node to inspect the real shape before assuming the key lookup is the problem).

"The value came out as the wrong type for what I need next." That's what to_type is for - don't leave it on default if the next node is strict about its input type. A number that arrived as a string from an API response is the single most common version of this problem; setting to_type to int or float fixes it at the source instead of needing a separate cast node afterward.

"My key has nested structure - I need a field inside a field." This node does one level of lookup per call. For nested JSON you'd chain two of these together (extract the outer object, then extract the inner field from that), since there's no dotted-path or nested-key syntax here.

CategoryEasyApi/Utils

Inputs (3)

NameTypeDefaultDescription
json*json对象(非数组类型)
keySTRING
to_typeCOMBOdefault5 options: default, str, int, float, bool

Outputs (1)

NameTypeDescription
any*