Import data from JSON files (2024)

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:

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 petsas 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 data from JSON files (1)

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:

Import data from JSON files (2)

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.

Import data from JSON files (2024)
Top Articles
Bathroom Cleaning Hacks: Tips and Tricks From a Professional Maid Service - The Maids
Active Equity Investing: Strategies
WALB Locker Room Report Week 5 2024
Spectrum Gdvr-2007
Craigslist Home Health Care Jobs
Asian Feels Login
Www.metaquest/Device Code
Bhad Bhabie Shares Footage Of Her Child's Father Beating Her Up, Wants Him To 'Get Help'
How to Watch Braves vs. Dodgers: TV Channel & Live Stream - September 15
Dark Souls 2 Soft Cap
2021 Tesla Model 3 Standard Range Pl electric for sale - Portland, OR - craigslist
Love Compatibility Test / Calculator by Horoscope | MyAstrology
Full Range 10 Bar Selection Box
Mephisto Summoners War
Kinkos Whittier
Chris Hipkins Fue Juramentado Como El Nuevo Primer Ministro De...
Jc Post News
charleston cars & trucks - by owner - craigslist
Chic Lash Boutique Highland Village
Crossword Nexus Solver
Nick Pulos Height, Age, Net Worth, Girlfriend, Stunt Actor
Hermitcraft Texture Pack
[Cheryll Glotfelty, Harold Fromm] The Ecocriticism(z-lib.org)
Marine Forecast Sandy Hook To Manasquan Inlet
Gazette Obituary Colorado Springs
Roane County Arrests Today
Living Shard Calamity
Mythical Escapee Of Crete
Kirk Franklin Mother Debra Jones Age
Watson 853 White Oval
Dr Seuss Star Bellied Sneetches Pdf
Nurtsug
Boondock Eddie's Menu
Leland Nc Craigslist
Peter Vigilante Biography, Net Worth, Age, Height, Family, Girlfriend
Craigslist Hamilton Al
9781644854013
Fapello.clm
Www.craigslist.com Waco
Kb Home The Overlook At Medio Creek
Despacito Justin Bieber Lyrics
Gamestop Store Manager Pay
R/Gnv
4Chan Zelda Totk
Ihop Deliver
Call2Recycle Sites At The Home Depot
Wwba Baseball
Morgan State University Receives $20.9 Million NIH/NIMHD Grant to Expand Groundbreaking Research on Urban Health Disparities
Itsleaa
Fetllife Com
How to Choose Where to Study Abroad
Latest Posts
Article information

Author: Van Hayes

Last Updated:

Views: 6007

Rating: 4.6 / 5 (46 voted)

Reviews: 85% of readers found this page helpful

Author information

Name: Van Hayes

Birthday: 1994-06-07

Address: 2004 Kling Rapid, New Destiny, MT 64658-2367

Phone: +512425013758

Job: National Farming Director

Hobby: Reading, Polo, Genealogy, amateur radio, Scouting, Stand-up comedy, Cryptography

Introduction: My name is Van Hayes, I am a thankful, friendly, smiling, calm, powerful, fine, enthusiastic person who loves writing and wants to share my knowledge and understanding with you.