Add JSONInputComponent to load JSON object as input

This commit is contained in:
Gabriel Luiz Freitas Almeida 2024-03-24 18:50:55 -03:00
commit 0c9126af4a

View file

@ -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