Add JSONInputComponent to load JSON object as input
This commit is contained in:
parent
8cedc611ad
commit
0c9126af4a
1 changed files with 30 additions and 0 deletions
30
src/backend/langflow/components/inputs/JSONInput.py
Normal file
30
src/backend/langflow/components/inputs/JSONInput.py
Normal 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
|
||||
Loading…
Add table
Add a link
Reference in a new issue