Loading JSON data into Neo4j database

Following on from recent article which describes method for using Neo4j database on Google CoLab. We will show how to load JSON data into a Neo4j database.

Prerequisites

Implementation

We will use Pandas and py2neo packages for the task in hand.

# save json data into pandas dataframe
import pandas as pd
json = pd.read_json("https://github.com/mneedham/neo4j-spark-chicago/raw/master/categories.json")

# create Neo4j session
from py2neo import Graph
graph = Graph("bolt://172.28.0.12:7687")

# load data
query = """
WITH $json AS document
UNWIND document.categories AS category
UNWIND category.sub_categories AS subCategory
MERGE (c:CrimeCategory {name: category.name})
MERGE (sc:SubCategory {code: subCategory.code})
ON CREATE SET sc.description = subCategory.description
MERGE (c)-[:CHILD]->(sc)
"""
graph.run(query,json=json)

# verify the load
q = '''
MATCH (SubCategory)
RETURN * LIMIT 5
'''
for a in graph.run(q):
  print(a)

Output

neo4j json data load