Skip to content

Latest commit

 

History

History
67 lines (57 loc) · 3.7 KB

README.md

File metadata and controls

67 lines (57 loc) · 3.7 KB

ComfyUI-JSON

Small set of utility nodes for working with JSON in ComfyUI: repair malformed JSON text, extract values with a simple path syntax, and serialize values back to text.

Category in ComfyUI: utils/json.

Installation

  • Copy this folder to ComfyUI/custom_nodes/ComfyUI-JSON and restart ComfyUI.

Nodes

JSON Repair

  • Display name: JSON Repair
  • Purpose: Repair and parse malformed JSON text. Optionally merge two inputs. Outputs a dict and a list (list-mappable).
  • Inputs
    • json_string (STRING, required): Raw text that likely contains JSON. The node extracts the first balanced object {...} or array [...] from the text. It also strips common code fences (json ... ).
    • json_string2 (STRING, optional): Second JSON text to merge. Dicts are merged into the object output; arrays are extended into the list output; primitives are appended to the list.
  • Outputs
    • JSON (DICT): Merged object result (empty if no objects were provided).
    • JSON Array (LIST, list-mapped): Merged array result. Emits [None] when empty to avoid ComfyUI list-mapping issues with empty lists.
  • How it parses
    1. Tries strict json.loads.
    2. Falls back to Python ast.literal_eval and normalizes to JSON-compatible types.
    3. If available, uses the optional json_repair library.
    4. If all fail, returns the original text (which will then be treated as a primitive and placed into the list output).
  • Tips
    • If your API/tool returns text with extra commentary or code blocks, feed it here first to get clean JSON.

JSON Property Extractor

  • Display name: JSON Property Extractor
  • Purpose: Extract a value from a JSON object (dict) using a simple JS-like path syntax.
  • Inputs
    • json (DICT, required): Parsed JSON object (dict). If you have text, parse it first (e.g., with JSON Repair).
    • property_path (STRING, optional): Path like .a[2].b or a.b[0]. Leave empty to return the whole object.
  • Outputs
    • value (*): Extracted value, type preserved (dict/list/string/number/bool/null).
    • value_type (STRING): Friendly type name (e.g., string, int, dict).
    • key (STRING): Last accessed key or index as a string.
  • Path syntax
    • Dot for identifiers: foo.bar
    • Brackets for indexes: [0], [-1]
    • Brackets with quotes for keys (supports dots/spaces): ["a.b"], ['key with spaces']
    • Whitespace in the path is ignored

JSON ➜ String

  • Display name: JSON ➜ String
  • Purpose: Convert any JSON-compatible value to a JSON string.
  • Inputs - json (*, required): Any JSON-compatible value (object, array, string, number, boolean, null). Non-serializable objects are serialized via str(...) as a fallback.
    • pretty (BOOLEAN, default True): Indent with 2 spaces.
    • ensure_ascii (BOOLEAN, default False): Escape non-ASCII characters when True.
      • parse_string_input (BOOLEAN, optional, default False): If the input is a string, try parsing it as JSON before serializing.
    • ignore_errors (BOOLEAN, optional, default False): On error, log and return {} instead of raising.
  • Outputs
    • json_string (STRING): The JSON text.

Example flows

  • Clean and parse, then extract a field
    1. JSON Repair → JSON Property Extractor with path like .items[0].name.
  • Serialize for logging or saving
    1. Upstream value → JSON ➜ String (enable Pretty for readability).

Troubleshooting

  • If JSON Property Extractor errors that input is not a dict, ensure you parsed text first (via JSON Repair).
  • If list-mapping on the array output of JSON Repair behaves oddly with empty inputs, note it emits [None] intentionally to avoid ComfyUI slicing errors.

— Inspired by the style of documentation in ComfyUI community repos.