如何导出并读取Neo4j Cypher查询的JSON输出?

huangapple go评论64阅读模式
英文:

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, &quot;r&quot;) as file:
        data = json.load(file)
    ---------------------------------------------------------------------------
    JSONDecodeError                           Traceback (most recent call last)
    Cell In[28], line 2
          1 with open(file_path, &quot;r&quot;) as file:
    ----&gt; 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 = &#39;test_records.json&#39; 
with open(file_path, &quot;r&quot;, encoding=&#39;utf-8-sig&#39;) as file:
    data = json.load(file)

print(data) 

Sample output:

[{&#39;n&#39;: {&#39;identity&#39;: 45, &#39;labels&#39;: [&#39;Person&#39;], &#39;properties&#39;: {&#39;name&#39;: &#39;Bob&#39;}}}, {&#39;n&#39;: {&#39;identity&#39;: 49, &#39;labels&#39;: [&#39;Person&#39;], &#39;properties&#39;: {&#39;name&#39;: &#39;Alice&#39;}}}, {&#39;n&#39;: {&#39;identity&#39;: 52, &#39;labels&#39;: [&#39;Person&#39;], &#39;properties&#39;: {&#39;name&#39;: &#39;Bob&#39;}}}, {&#39;n&#39;: {&#39;identity&#39;: 53, &#39;labels&#39;: [&#39;Person&#39;], &#39;properties&#39;: {&#39;name&#39;: &#39;Alice&#39;}}}]

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)]

huangapple
  • 本文由 发表于 2023年6月22日 19:42:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/76531524.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定