community: fixes json loader not getting texts with json standard (#27327)

This PR fixes JSONLoader._get_text not converting objects to json string
correctly.
If an object is serializable and is not a dict, JSONLoader will use
python built-in str() method to convert it to string. This may cause
object converted to strings not following json standard. For example, a
list will be converted to string with single quotes, and if json.loads
try to load this string, it will cause error.

---------

Co-authored-by: Erick Friis <erick@langchain.dev>
This commit is contained in:
Botong Zhu
2024-12-13 03:33:45 +08:00
committed by GitHub
parent 4149c0dd8d
commit 13c3c4a210
2 changed files with 14 additions and 13 deletions

View File

@@ -188,7 +188,7 @@ class JSONLoader(BaseLoader):
# In case the text is None, set it to an empty string
elif isinstance(content, str):
return content
elif isinstance(content, dict):
elif isinstance(content, (dict, list)):
return json.dumps(content) if content else ""
else:
return str(content) if content is not None else ""