From 0c9126af4a247a27b4e3971266cd26e81fe3faba Mon Sep 17 00:00:00 2001 From: Gabriel Luiz Freitas Almeida Date: Sun, 24 Mar 2024 18:50:55 -0300 Subject: [PATCH] Add JSONInputComponent to load JSON object as input --- .../langflow/components/inputs/JSONInput.py | 30 +++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/backend/langflow/components/inputs/JSONInput.py diff --git a/src/backend/langflow/components/inputs/JSONInput.py b/src/backend/langflow/components/inputs/JSONInput.py new file mode 100644 index 000000000..752f41aa1 --- /dev/null +++ b/src/backend/langflow/components/inputs/JSONInput.py @@ -0,0 +1,30 @@ +import ast +import json + +from langflow import CustomComponent +from langflow.schema import Record + + +class JSONInputComponent(CustomComponent): + display_name = "JSON Input" + description = "Load a JSON object as input." + + def build_config(self): + return { + "json_str": { + "display_name": "JSON String", + "multiline": True, + "info": "The JSON string to load.", + } + } + + def build(self, json_str: str) -> Record: + try: + data = json.loads(json_str) + except json.JSONDecodeError: + try: + data = ast.literal_eval(json_str) + except (SyntaxError, ValueError): + raise ValueError("Invalid JSON string.") + record = Record(data=data) + return record