Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions haystack/components/converters/json.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ def _get_content_and_meta(self, source: ByteStream) -> list[tuple[str, dict[str,
logger.warning("Expected a scalar value but got {obj}. Skipping it.", obj=obj)
continue

if text is not None and not isinstance(text, str):
Comment thread
sjrl marked this conversation as resolved.
text = str(text)

meta = {}
if meta_fields == "*":
meta = {k: v for k, v in obj.items() if k != self._content_key}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
fixes:
- |
Fixed ``JSONConverter`` failing with ``ValueError`` when ``content_key`` contains numeric or boolean scalar values.
These values are now converted to strings before creating the ``Document``, while ``null`` values remain unchanged.
10 changes: 10 additions & 0 deletions test/components/converters/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -511,6 +511,16 @@ def test_run_with_content_key(tmpdir):
assert result["documents"][2].meta == {}


@pytest.mark.parametrize("value, expected", [(123, "123"), (1.5, "1.5"), (True, "True"), (None, None)])
def test_run_with_content_key_with_scalar_values(value, expected):
source = ByteStream.from_string(json.dumps({"body": value}))

converter = JSONConverter(content_key="body")
result = converter.run(sources=[source])

assert result["documents"][0].content == expected


def test_run_with_content_key_and_extra_meta_fields(tmpdir):
first_test_file = Path(tmpdir / "first_test_file.json")
second_test_file = Path(tmpdir / "second_test_file.json")
Expand Down
Loading