英文:
How to export and read json output from neo4j cypher queries?
问题
在运行Neo4j上的Cypher查询后,我可以将结果导出/下载为JSON或CSV格式。然而,当我尝试将其导入Python时,我发现JSON和CSV格式不正确。有人可以帮助我导入JSON吗?谢谢。
with open(file_path, "r") as file:
data = json.load(file)
JSONDecodeError 错误
Traceback (most recent call last)
Cell In[28], line 2
1 with open(file_path, "r") as file:
----> 2 data = json.load(file)
<details>
<summary>英文:</summary>
After running cypher query on neo4j, I can export/download the result as json or csv format. However, as I tried to import that into python, I found error as the json and csv are not formatted properly. Can someone please help with importing into json? Thank you.
with open(file_path, "r") as file:
data = json.load(file)
---------------------------------------------------------------------------
JSONDecodeError Traceback (most recent call last)
Cell In[28], line 2
1 with open(file_path, "r") as file:
----> 2 data = json.load(file)
</details>
# 答案1
**得分**: 1
从neo4j浏览器导出的文件使用了不同的编码。请使用"utf-8-sig"作为编码。
```python
import json
file_path = 'test_records.json'
with open(file_path, "r", encoding='utf-8-sig') as file:
data = json.load(file)
print(data)
示例输出:
[{'n': {'identity': 45, 'labels': ['Person'], 'properties': {'name': 'Bob'}}}, {'n': {'identity': 49, 'labels': ['Person'], 'properties': {'name': 'Alice'}}}, {'n': {'identity': 52, 'labels': ['Person'], 'properties': {'name': 'Bob'}}}, {'n': {'identity': 53, 'labels': ['Person'], 'properties': {'name': 'Alice'}}}]
以下是我的笔记本和Python版本:
笔记本服务器的版本是:6.5.4
服务器运行的Python版本是:
Python 3.9.6 (默认,2022年10月18日,12:41:40)
[Clang 14.0.0 (clang-1400.0.29.202)]
英文:
The exported file from neo4j browser is using a different encoding. Please use "utf-8-sig" as encoding.
import json
file_path = 'test_records.json'
with open(file_path, "r", encoding='utf-8-sig') as file:
data = json.load(file)
print(data)
Sample output:
[{'n': {'identity': 45, 'labels': ['Person'], 'properties': {'name': 'Bob'}}}, {'n': {'identity': 49, 'labels': ['Person'], 'properties': {'name': 'Alice'}}}, {'n': {'identity': 52, 'labels': ['Person'], 'properties': {'name': 'Bob'}}}, {'n': {'identity': 53, 'labels': ['Person'], 'properties': {'name': 'Alice'}}}]
Below are my notebook and python versions:
The version of the notebook server is: 6.5.4
The server is running on this version of Python:
Python 3.9.6 (default, Oct 18 2022, 12:41:40)
[Clang 14.0.0 (clang-1400.0.29.202)]
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论