Skip to content

Commit

Permalink
fix: ParseJSONDataComponent prevent unnecessary array wrapping (langf…
Browse files Browse the repository at this point in the history
…low-ai#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>
  • Loading branch information
2 people authored and diogocabral committed Nov 26, 2024
1 parent 4ad6c3e commit de86a47
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 8 deletions.
26 changes: 20 additions & 6 deletions src/backend/base/langflow/components/processing/parse_json_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,22 +50,36 @@ def filter_data(self) -> list[Data]:
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)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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'}",
)
Expand All @@ -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}]}',
)
Expand Down

0 comments on commit de86a47

Please sign in to comment.