citykid
citykid

Reputation: 11132

How can I import a simple json dataset into memgraph

I have read all information about importing json but find that documentation poor. The memgraph docs say, just do this:

CALL json_util.load_from_url("https://download.memgraph.com/asset/mage/data.json") 
YIELD objects
UNWIND objects AS o
RETURN o.first_name AS name, o.last_name AS surname;

But that does not do it, because a typical json graph dataset has several properties holding nodes and links with arbitrary properties:

{
   nodes: [ ... ],
   links: [ ... ],
   ...
}

So we need to access these properties and we need to create nodes with all properties of the imported objects. I cant find any documentation on how to do this. I finally managed to create nodes inside memgraph with the correct type ("ENTITY") but do not see how I can assign those nodes all properties from the imported json data:

CALL json_util.load_from_path("/data.json")
YIELD objects              // "objects" is the named result from the function above
UNWIND objects as o        // create a list of the properties of the imported result, which is a single object in my case and in most cases
UNWIND o.nodes as n        // select the nodes and return them as list
CREATE (e:ENTITY { id:n.id }) // all nodes will have 1 property, "id"
RETURN e

All nodes will have 1 property, "id", but I need all properties. How is this done. Where is the documentation or the book that explains that?

In javascript I would write

    CREATE (e:ENTITY { ...n }) 

How is this done in Cypher? Or how can this done in memgraph with another language or tool?

Upvotes: 1

Views: 358

Answers (1)

KateLatte
KateLatte

Reputation: 728

If you use json_util.load_from_path procedure, then you have to specify each of the properties you want to write to the database, just like in the example in Memgraph docs:

CALL json_util.load_from_path("path/to/data.json")
YIELD objects
UNWIND objects AS o
CREATE (:Person {first_name: o.first_name, last_name: o.last_name, pets: o.pets});

The above procedure is expecting classic JSON, not structured in any particular way.

On the other hand, the import_util.json() procedure expects JSON to be in a specific format, structured the same as the JSON file that the export_util.json() procedure generates. Check the example in Memgraph docs.

If you wish to import your data in a way they are structured now, you need to use json_util.load_from_path() procedure and write down all of the properties you want to create in the database. Otherwise, you can structure your data appropriately for the import_util.json() procedure and import it that way. The construct you mentioned from the JavaScript is not supported in Cypher.

Let's suppose other properties you have in the JSON file are name and last_name. Then you would run this:

CALL json_util.load_from_path("/data.json")
YIELD objects              // "objects" is the named result from the function above
UNWIND objects as o        // create a list of the properties of the imported result, which is a single object in my case and in most cases
UNWIND o.nodes as n        // select the nodes and return them as list
CREATE (e:ENTITY { id:n.id, name: n.name, last_name: n.last_name }) 
RETURN e

We fixed the import docs in the next version, and let me know if that's more clear now. Here is also the JSON import subpage.

Upvotes: 2

Related Questions