JSON
A JSON file is a file that stores simple data structures and objects inJavaScript Object Notation format, which is a standard data interchange format.The data you want to import to the database is often saved in JSON format, andyou might want to import parts of that data as graph objects - nodes orrelationships.
Data can be imported using query modules implemented in the MAGE library:
json_util
query moduleimport_util
query module.
The difference is that json_util.load_from_path()
has no requirements aboutthe formatting of data inside the JSON file, while the import_util.json()
procedure requires data to be formatted in a specific way. It is the sameformatting the export_util.json()
procedure generates when it's used to exportdata from Memgraph into a JSON file.
JSON file data format required by the import_util.json()
procedure:
[ { "id": 6114, "labels": [ "Person" ], "properties": { "name": "Anna" }, "type": "node" }, { "id": 6115, "labels": [ "Person" ], "properties": { "name": "John" }, "type": "node" }, { "id": 6116, "labels": [ "Person" ], "properties": { "name": "Kim" }, "type": "node" }, { "end": 6115, "id": 21120, "label": "IS_FRIENDS_WITH", "properties": {}, "start": 6114, "type": "relationship" }, { "end": 6116, "id": 21121, "label": "IS_FRIENDS_WITH", "properties": {}, "start": 6114, "type": "relationship" }, { "end": 6116, "id": 21122, "label": "IS_MARRIED_TO", "properties": {}, "start": 6115, "type": "relationship" }]
To be able to call the procedures, you need to install MAGE graph library andload query modules.
The library is already a part of the memgraph-platform and memgraph-mage Dockerimages.
💡
If you can choose the format of the data you want to import, the fastest way toimport data into Memgraph is from a CSV file using the LOAD CSVclause. Learn more about the shortest path to import datainto Memgraph by reading best practices forimport.
Examples
Below, you can find two examples of how to load data from a JSON file dependingon the file location:
Load JSON from a local file
To import data from a local JSON file, you can use thejson_util.load_from_path()
procedureor import_util.json()
procedure.
The difference is that json_util.load_from_path()
has no requirements aboutthe formatting of data inside the JSON file while the import_util.json()
procedure does. It is the same formatting the export_util.json()
proceduregenerates when it's used to export data from Memgraph into a JSON file.
json_util.load_from_path()
procedure
The json_util.load_from_path()
procedure takes one string argument (path
)and returns a list of JSON objects from the file located at the provided path.
Let's import data from a file data.json
with the following content:
{ "first_name": "Jessica", "last_name": "Rabbit", "pets": ["dog", "cat", "bird"]}
If you are using Docker to run Memgraph, you will need to copy the files fromyour local directory into theDockercontainer where Memgraph can access them.
To create a node with the label Person
and first_name
, last_name
and pets
as properties, run the following query:
CALL json_util.load_from_path("path/to/data.json")YIELD objectsUNWIND objects AS oCREATE (:Person {first_name: o.first_name, last_name: o.last_name, pets: o.pets});
After you execute the above query, the graph in Memgraph should look like this:
import_util.json()
procedure
To find out how to import data with the import_util.json()
procedure checkout the MAGE documentation.
Load JSON from a remote address
To import data from a remote JSON file, use load_from_url(url)
procedure thattakes one string argument (url
) and returns a list of JSON objects from thefile located at the provided URL.
For example, at "https://download.memgraph.com/asset/mage/data.json"
, you canfind the following data.json
file:
{ "first_name": "James", "last_name": "Bond", "pets": ["dog", "cat", "fish"]}
To create a node with the label Person
and first_name
, last_name
andpets
as properties from the data.json
file. You can run the following query:
CALL json_util.load_from_url("https://download.memgraph.com/asset/mage/data.json")YIELD objectsUNWIND objects AS oCREATE (:Person {first_name: o.first_name, last_name: o.last_name, pets: o.pets});
After you run the above query, the graph in Memgraph should look like this:
To load JSON files from another local or remote location, just replace theargument of the procedure with the appropriate path or URL. If you want tocreate a different kind of graph, you need to change the queryaccordingly.