Trying to Use neo4j.apoc to load json file as data into a graph using python

1.4k views Asked by At

I am new to using neo4j through python. I wanted to use APOC to read in a json file using python and populate a graph database. The JSON file is already in a neo4j required structure to create nodes and relationships.

When running the query CALL apoc.load.json("file:///xyz.json") in neo4j Browser it runs fine and populates the nodes and relationships. But when I do it in python using py2neo it prints out the json file and nothing is added to the database when I check it in browser.

Python Code:

`from py2neo import Graph

g = Graph("bolt://localhost:7687", auth=("neo4j", "password"))

query = """
CALL apoc.load.json("file:///xyz.json")
"""

g.run(query)  
`
The Json file is kept in import folder of the db.
2

There are 2 answers

0
Thennan On

As mentioned in the Neo4j APOC documentation, apoc.load.json just reads the json file and returns a map object. You will have to add further steps for node and relationship creation.
It looks like the query you had run in the neo4j browser had more to it. Or you might have mistaken a different Result frame to be from the json load apoc.

0
Tim B On

I wrote a python module called "dict2graph" for exactly that. Loading json into neo4j without thinking.

https://dzd-ev.github.io/dict2graph-docs/

import json
from dict2graph import Dict2graph
from neo4j import GraphDatabase

# connect to our neo4j database
NEO4J_DRIVER = GraphDatabase.driver("neo4j://localhost")

with open('data.json') as f:
    data = json.load(f.read())

# Parse and load the data into neo4j ("merge()" is possible as well)
Dict2graph().parse(data).create(NEO4J_DRIVER)