Skip to content

The most powerful and modular diffusion model GUI, api and backend with a graph/nodes interface. The fastest local inference engine in the world.

JsonExtractString can now handle JSON with prefix and/or suffix. (#16439)

comfyanonymousb0f4b7b294ce
diff --git a/comfy_extras/nodes_string.py b/comfy_extras/nodes_string.py
index 21929ae63..553301b3f 100644
--- a/comfy_extras/nodes_string.py
+++ b/comfy_extras/nodes_string.py
@@ -415,6 +415,7 @@ class JsonExtractString(io.ComfyNode):
display_name="Extract Text from JSON",
category="text",
search_aliases=["json", "extract json", "parse json", "json value", "read json"],
+ description="Extract a key's value from the first valid JSON object in the text, including Markdown code blocks.",
inputs=[
io.String.Input("json_string", multiline=True),
io.String.Input("key", multiline=False),
@@ -426,19 +427,17 @@ class JsonExtractString(io.ComfyNode):
@classmethod
def execute(cls, json_string, key):
- try:
- data = json.loads(json_string)
- if isinstance(data, dict) and key in data:
- value = data[key]
- if value is None:
- return io.NodeOutput("")
-
- return io.NodeOutput(str(value))
+ decoder = json.JSONDecoder()
+ for match in re.finditer(r"\{", json_string):
+ try:
+ data, _ = decoder.raw_decode(json_string, match.start())
+ except json.JSONDecodeError:
+ continue
- return io.NodeOutput("")
+ value = data.get(key)
+ return io.NodeOutput("" if value is None else str(value))
- except (json.JSONDecodeError, TypeError):
- return io.NodeOutput("")
+ return io.NodeOutput("")
def _dump_json(value, indent):