mirror of
https://github.com/comfyanonymous/ComfyUI.git
synced 2026-04-23 17:02:38 +08:00
Add JsonExtractString node.
This commit is contained in:
parent
b41ab53b6f
commit
aee17c54b9
@ -1,4 +1,5 @@
|
|||||||
import re
|
import re
|
||||||
|
import json
|
||||||
from typing_extensions import override
|
from typing_extensions import override
|
||||||
|
|
||||||
from comfy_api.latest import ComfyExtension, io
|
from comfy_api.latest import ComfyExtension, io
|
||||||
@ -375,6 +376,39 @@ class RegexReplace(io.ComfyNode):
|
|||||||
return io.NodeOutput(result)
|
return io.NodeOutput(result)
|
||||||
|
|
||||||
|
|
||||||
|
class JsonExtractString(io.ComfyNode):
|
||||||
|
@classmethod
|
||||||
|
def define_schema(cls):
|
||||||
|
return io.Schema(
|
||||||
|
node_id="JsonExtractString",
|
||||||
|
display_name="Extract String from JSON",
|
||||||
|
category="utils/string",
|
||||||
|
search_aliases=["json", "extract json", "parse json", "json value", "read json"],
|
||||||
|
inputs=[
|
||||||
|
io.String.Input("json_string", multiline=True),
|
||||||
|
io.String.Input("key", multiline=False),
|
||||||
|
],
|
||||||
|
outputs=[
|
||||||
|
io.String.Output(),
|
||||||
|
]
|
||||||
|
)
|
||||||
|
|
||||||
|
@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))
|
||||||
|
|
||||||
|
return io.NodeOutput("")
|
||||||
|
|
||||||
|
except (json.JSONDecodeError, TypeError):
|
||||||
|
return io.NodeOutput("")
|
||||||
|
|
||||||
class StringExtension(ComfyExtension):
|
class StringExtension(ComfyExtension):
|
||||||
@override
|
@override
|
||||||
async def get_node_list(self) -> list[type[io.ComfyNode]]:
|
async def get_node_list(self) -> list[type[io.ComfyNode]]:
|
||||||
@ -390,6 +424,7 @@ class StringExtension(ComfyExtension):
|
|||||||
RegexMatch,
|
RegexMatch,
|
||||||
RegexExtract,
|
RegexExtract,
|
||||||
RegexReplace,
|
RegexReplace,
|
||||||
|
JsonExtractString,
|
||||||
]
|
]
|
||||||
|
|
||||||
async def comfy_entrypoint() -> StringExtension:
|
async def comfy_entrypoint() -> StringExtension:
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user