top of page
  • Writer's pictureRevanth Reddy Tondapu

Part 23: Loading Data from a JSON File into Neo4j: A Step-by-Step Guide


Loading Data from a JSON File into Neo4j
Loading Data from a JSON File into Neo4j

In this blog post, we will walk through the process of loading data into a Neo4j graph database from a JSON file. We will cover the steps required to clean the database, install necessary plugins, load the JSON data, create nodes, and verify the data insertion.


Deleting Existing Nodes and Relationships

First, we need to ensure that our database is clean by deleting any existing nodes and their relationships. Open the Neo4j browser connected to your active DBMS and run the following Cypher query:

MATCH (n) DETACH DELETE n;

This command will delete all nodes and relationships, giving us a fresh start.


Confirming the JSON Plugin Installation

Before loading data from a JSON file, we need to confirm that the appropriate plugin for handling JSON files is installed. This plugin is crucial for executing procedures that allow us to load JSON data into Neo4j.


Inspecting the JSON File

Next, let's navigate to the import directory where our JSON file, sample_data.json, is located. This file contains simple records about people's names and their birth years. Here is an example of what the JSON file looks like:

[
  {
    "name": "Keanu Reeves",
    "born": 1964
  },
  {
    "name": "Carrie-Anne Moss",
    "born": 1967
  },
  {
    "name": "Laurence Fishburne",
    "born": 1961
  },
  {
    "name": "Hugo Weaving",
    "born": 1960
  }
]

Understanding the Cypher Query for JSON Data

Let's break down the Cypher query that will load data from the JSON file and create nodes in the Neo4j database:

CALL apoc.load.json("file:///sample_data.json") YIELD value
UNWIND value AS person
CREATE (p:Person {name: person.name, born: person.born});

Explanation of the Query

  1. CALL apoc.load.json: This line uses the APOC library's apoc.load.json procedure to load data from the specified JSON file.

  2. YIELD value: This clause returns each JSON object as a record, which we can further process.

  3. UNWIND value AS person: This clause is used to iterate over each JSON object returned by the previous step. We alias each object as person for easier reference.

  4. CREATE (p:Person): For each person object, a new node with the label Person is created. The properties name and born are set to the corresponding values from the JSON object.


Running the Query

Execute the above Cypher query in the Neo4j browser. If you encounter an error, it may indicate that a specific property needs to be set in the configuration file.


Updating the Configuration File

To enable the loading of JSON files, we need to add a property to the Neo4j configuration file. Navigate to the Neo4j home directory and locate the config folder. Create or update the configuration file (neo4j.conf) with the following property:

apoc.import.file.enabled=true

After updating the configuration file, restart the Neo4j DBMS to apply the changes.


Re-running the Cypher Query

Once the DBMS is restarted, re-run the Cypher query:

CALL apoc.load.json("file:///sample_data.json") YIELD value
UNWIND value AS person
CREATE (p:Person {name: person.name, born: person.born});

This time, the query should execute successfully, creating new Person nodes in the database based on the JSON data.


Verifying the Inserted Data

To verify the data insertion, run the following match query:

MATCH (p:Person)
RETURN p;

This query will return all Person nodes in the database. You should see four nodes, each with properties name and born, corresponding to the data in the JSON file.


Conclusion

In this guide, we learned how to load data from a JSON file into Neo4j using Cypher queries and the APOC library. By following these steps, you can efficiently manage and analyze your data in Neo4j, leveraging its powerful graph database capabilities. Whether you are working with small datasets or large-scale data, these techniques will help you integrate JSON data seamlessly into your Neo4j environment.

1 view0 comments

Comments


bottom of page