Added TextToJSON node

This commit is contained in:
Lukas Julian Lambertz
2025-06-21 14:43:24 +02:00
parent 27e8c783be
commit ce17e2fd60
3 changed files with 94 additions and 44 deletions
+4
View File
@@ -9,17 +9,21 @@ from .nodes.utilities import UTILITY_NODE_CLASS_MAPPINGS as UTILITY_NODES
from .nodes.utilities import UTILITY_NODE_DISPLAY_NAME_MAPPINGS as UTILITY_DISPLAY_NAMES
from .nodes.timing import TIMING_NODE_CLASS_MAPPINGS as TIMING_NODES
from .nodes.timing import TIMING_NODE_DISPLAY_NAME_MAPPINGS as TIMING_DISPLAY_NAMES
from .nodes.json import NODE_CLASS_MAPPINGS as JSON_NODES
from .nodes.json import NODE_DISPLAY_NAME_MAPPINGS as JSON_DISPLAY_NAMES
# Combine all node mappings
NODE_CLASS_MAPPINGS = {}
NODE_CLASS_MAPPINGS.update(TEXT_NODES)
NODE_CLASS_MAPPINGS.update(UTILITY_NODES)
NODE_CLASS_MAPPINGS.update(TIMING_NODES)
NODE_CLASS_MAPPINGS.update(JSON_NODES)
NODE_DISPLAY_NAME_MAPPINGS = {}
NODE_DISPLAY_NAME_MAPPINGS.update(TEXT_DISPLAY_NAMES)
NODE_DISPLAY_NAME_MAPPINGS.update(UTILITY_DISPLAY_NAMES)
NODE_DISPLAY_NAME_MAPPINGS.update(TIMING_DISPLAY_NAMES)
NODE_DISPLAY_NAME_MAPPINGS.update(JSON_DISPLAY_NAMES)
__all__ = ['NODE_CLASS_MAPPINGS', 'NODE_DISPLAY_NAME_MAPPINGS']
+90
View File
@@ -0,0 +1,90 @@
import json
class TextToJSON:
"""
Node for converting a text string to a JSON object.
Attempts to fix invalid JSON if possible.
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text_input": ("STRING", {
"multiline": True,
"default": '{"key": "value"}'
}),
}
}
RETURN_TYPES = ("JSON", "STRING")
RETURN_NAMES = ("json_output", "error_message")
FUNCTION = "convert_to_json"
CATEGORY = "DebugPadawan/JSON"
def convert_to_json(self, text_input):
"""
Convert a text string to a JSON object. Attempts to fix invalid JSON.
Args:
text_input (str): Input text to convert
Returns:
tuple: (JSON object, error message)
"""
try:
# Parse the text input as JSON
json_output = json.loads(text_input)
return (json_output, "")
except json.JSONDecodeError as e:
# Attempt to fix common JSON issues
fixed_json = self.try_fix_json(text_input)
if fixed_json:
try:
json_output = json.loads(fixed_json)
return (json_output, "JSON was corrected.")
except json.JSONDecodeError:
pass # If fixing fails, fall through to error handling
# Return an error message if the JSON is invalid
return (None, f"Invalid JSON: {str(e)}")
except Exception as e:
# Handle other exceptions
return (None, f"Error: {str(e)}")
def try_fix_json(self, text_input):
"""
Attempt to fix common JSON formatting issues.
Args:
text_input (str): Input text to fix
Returns:
str: Fixed JSON string, or None if it cannot be fixed
"""
try:
# Replace single quotes with double quotes
fixed = text_input.replace("'", '"')
# Add missing commas between JSON objects or arrays
fixed = fixed.replace("}{", "},{")
# Ensure the JSON starts and ends with braces or brackets
if not (fixed.startswith("{") or fixed.startswith("[")):
fixed = "{" + fixed
if not (fixed.endswith("}") or fixed.endswith("]")):
fixed = fixed + "}"
return fixed
except Exception:
return None
NODE_CLASS_MAPPINGS = {
"DebugPadawan_TextToJSON": TextToJSON,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"DebugPadawan_TextToJSON": "Text to JSON",
}
-44
View File
@@ -156,60 +156,16 @@ class ListInfo:
return (count, first_item, last_item)
class TextToJSON:
"""
Node for converting a text string to a JSON object
"""
@classmethod
def INPUT_TYPES(cls):
return {
"required": {
"text_input": ("STRING", {
"multiline": True,
"default": '{"key": "value"}'
}),
}
}
RETURN_TYPES = ("JSON", "STRING")
RETURN_NAMES = ("json_output", "error_message")
FUNCTION = "convert_to_json"
CATEGORY = "DebugPadawan/JSON"
def convert_to_json(self, text_input):
"""
Convert a text string to a JSON object
Args:
text_input (str): Input text to convert
Returns:
tuple: (JSON object, error message)
"""
try:
# Parse the text input as JSON
json_output = json.loads(text_input)
return (json_output, "")
except json.JSONDecodeError as e:
# Return an error message if the JSON is invalid
return (None, f"Invalid JSON: {str(e)}")
except Exception as e:
# Handle other exceptions
return (None, f"Error: {str(e)}")
NODE_CLASS_MAPPINGS = {
"DebugPadawan_TextSplitter": TextSplitter,
"DebugPadawan_TextJoiner": TextJoiner,
"DebugPadawan_ListInfo": ListInfo,
"DebugPadawan_TextToJSON": TextToJSON,
}
NODE_DISPLAY_NAME_MAPPINGS = {
"DebugPadawan_TextSplitter": "Text Splitter",
"DebugPadawan_TextJoiner": "Text Joiner",
"DebugPadawan_ListInfo": "List Info",
"DebugPadawan_TextToJSON": "Text to JSON",
}