fix: ParseJSONDataComponent prevent unnecessary array wrapping (#4357)
* fix(ParseJSONDataComponent): prevent unnecessary array wrapping Prevent wrapping non-array inputs in arrays to ensure correct JSON parsing and filtering. * [autofix.ci] apply automated fixes * updated failing test --------- Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
This commit is contained in:
parent
a107650cc6
commit
19963ded26
2 changed files with 22 additions and 8 deletions
|
|
@ -50,22 +50,36 @@ class ParseJSONDataComponent(Component):
|
|||
to_filter = self.input_value
|
||||
if not to_filter:
|
||||
return []
|
||||
# Check if input is a list
|
||||
if isinstance(to_filter, list):
|
||||
to_filter = [self._parse_data(f) for f in to_filter]
|
||||
else:
|
||||
to_filter = [self._parse_data(to_filter)]
|
||||
to_filter = self._parse_data(to_filter)
|
||||
|
||||
to_filter = [repair_json(f) for f in to_filter]
|
||||
to_filter_as_dict = []
|
||||
for f in to_filter:
|
||||
# If input is not a list, don't wrap it in a list
|
||||
if not isinstance(to_filter, list):
|
||||
to_filter = repair_json(to_filter)
|
||||
try:
|
||||
to_filter_as_dict.append(json.loads(f))
|
||||
to_filter_as_dict = json.loads(to_filter)
|
||||
except JSONDecodeError:
|
||||
try:
|
||||
to_filter_as_dict.append(json.loads(repair_json(f)))
|
||||
to_filter_as_dict = json.loads(repair_json(to_filter))
|
||||
except JSONDecodeError as e:
|
||||
msg = f"Invalid JSON: {e}"
|
||||
raise ValueError(msg) from e
|
||||
else:
|
||||
to_filter = [repair_json(f) for f in to_filter]
|
||||
to_filter_as_dict = []
|
||||
for f in to_filter:
|
||||
try:
|
||||
to_filter_as_dict.append(json.loads(f))
|
||||
except JSONDecodeError:
|
||||
try:
|
||||
to_filter_as_dict.append(json.loads(repair_json(f)))
|
||||
except JSONDecodeError as e:
|
||||
msg = f"Invalid JSON: {e}"
|
||||
raise ValueError(msg) from e
|
||||
to_filter = to_filter_as_dict
|
||||
|
||||
full_filter_str = json.dumps(to_filter_as_dict)
|
||||
|
||||
|
|
|
|||
|
|
@ -37,7 +37,7 @@ async def test_from_message():
|
|||
ParseJSONDataComponent,
|
||||
inputs={
|
||||
"input_value": ComponentInputHandle(clazz=ChatInput, inputs={}, output_name="message"),
|
||||
"query": ".[0].key",
|
||||
"query": ".key",
|
||||
},
|
||||
run_input="{'key':'value1'}",
|
||||
)
|
||||
|
|
@ -47,7 +47,7 @@ async def test_from_message():
|
|||
ParseJSONDataComponent,
|
||||
inputs={
|
||||
"input_value": ComponentInputHandle(clazz=ChatInput, inputs={}, output_name="message"),
|
||||
"query": ".[0].key.[0].field2",
|
||||
"query": ".key.[0].field2",
|
||||
},
|
||||
run_input='{"key":[{"field1": 1, "field2": 2}]}',
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue