From 27e8c783be4ee1603d327f6300cfe93a912da084 Mon Sep 17 00:00:00 2001 From: Lukas Julian Lambertz Date: Sat, 21 Jun 2025 14:26:57 +0200 Subject: [PATCH] Update text_processing.py --- nodes/text_processing.py | 49 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 49 insertions(+) diff --git a/nodes/text_processing.py b/nodes/text_processing.py index 4f920b4..ab8e983 100644 --- a/nodes/text_processing.py +++ b/nodes/text_processing.py @@ -1,3 +1,6 @@ +import json + + class TextSplitter: """ Node for splitting text strings by a delimiter @@ -153,14 +156,60 @@ 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", } \ No newline at end of file