Skip to content

Commit 1391579

Browse files
Add JsonExtractString node. (Comfy-Org#13435)
1 parent d0c53c5 commit 1391579

1 file changed

Lines changed: 35 additions & 0 deletions

File tree

comfy_extras/nodes_string.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import re
2+
import json
23
from typing_extensions import override
34

45
from comfy_api.latest import ComfyExtension, io
@@ -375,6 +376,39 @@ def execute(cls, string, regex_pattern, replace, case_insensitive=True, multilin
375376
return io.NodeOutput(result)
376377

377378

379+
class JsonExtractString(io.ComfyNode):
380+
@classmethod
381+
def define_schema(cls):
382+
return io.Schema(
383+
node_id="JsonExtractString",
384+
display_name="Extract String from JSON",
385+
category="utils/string",
386+
search_aliases=["json", "extract json", "parse json", "json value", "read json"],
387+
inputs=[
388+
io.String.Input("json_string", multiline=True),
389+
io.String.Input("key", multiline=False),
390+
],
391+
outputs=[
392+
io.String.Output(),
393+
]
394+
)
395+
396+
@classmethod
397+
def execute(cls, json_string, key):
398+
try:
399+
data = json.loads(json_string)
400+
if isinstance(data, dict) and key in data:
401+
value = data[key]
402+
if value is None:
403+
return io.NodeOutput("")
404+
405+
return io.NodeOutput(str(value))
406+
407+
return io.NodeOutput("")
408+
409+
except (json.JSONDecodeError, TypeError):
410+
return io.NodeOutput("")
411+
378412
class StringExtension(ComfyExtension):
379413
@override
380414
async def get_node_list(self) -> list[type[io.ComfyNode]]:
@@ -390,6 +424,7 @@ async def get_node_list(self) -> list[type[io.ComfyNode]]:
390424
RegexMatch,
391425
RegexExtract,
392426
RegexReplace,
427+
JsonExtractString,
393428
]
394429

395430
async def comfy_entrypoint() -> StringExtension:

0 commit comments

Comments
 (0)