diff --git a/src/backend/base/langflow/components/processing/parse_json_data.py b/src/backend/base/langflow/components/processing/parse_json_data.py index 35b3ced69..0881b0b94 100644 --- a/src/backend/base/langflow/components/processing/parse_json_data.py +++ b/src/backend/base/langflow/components/processing/parse_json_data.py @@ -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) diff --git a/src/backend/tests/integration/components/helpers/test_parse_json_data.py b/src/backend/tests/integration/components/helpers/test_parse_json_data.py index aed3311da..63820d28b 100644 --- a/src/backend/tests/integration/components/helpers/test_parse_json_data.py +++ b/src/backend/tests/integration/components/helpers/test_parse_json_data.py @@ -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}]}', )