# How to form a graph DB from relation object

**URL:** <https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779>\
**Category:** Neo4j Graph Platform\
**Tags:** migrated, python-tagged\
**Created:** [December 20, 2022, 6:56am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779 "2022-12-20T06:56:25Z")\
**Posts on this page:** 20\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 20, 2022, 6:56am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/1 "2022-12-20T06:56:25Z")

</div>

I wrote a python code which will extract meaningful relation from a given text as an input.

Please check the below image for better understanding

![Jupyter123_0-1671519287785.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/2/d/2d538cb269f7c11a2c7d4699571e2cc9df1551fd.png "Jupyter123\_0-1671519287785.png")

The above image show that, I extracted the relation from the text

Now i want to move those relation object to neo4j and form a graph?

how do i do that? what is the query for that?

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 20, 2022, 9:40am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/2 "2022-12-20T09:40:07Z")

</div>

hi [@Estelle](https://community.neo4j.com/t5/user/viewprofilepage/user-id/6250) , thanks for the response.

Well, **Instead of converting the object to JSON**

Is it possible to do this via **python object**?

**Pls note that:** I already connected python code and neo4j by this command:

**from neo4j import GraphDatabase**

**data\_base\_connection1 = GraphDatabase.driver(uri = "bolt://localhost:7687", auth=("neo4j", "password"))**  
**session1 = data\_base\_connection1.session()**

In my python program i have a class called KB and inside that class i have a method called def print(self): and some logic inside that..

So whenever i type KB.print()

i get relation object [the one that i shown in above image]

so what would be the query to send that relation object from python to neo4j and form a graph?

---

<div class="post-metadata">

**Author:** ![Estelle](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/estelle/32/33070_2.png) [@Estelle](https://community.neo4j.com/u/Estelle)\
**Post date:** [December 20, 2022, 8:40am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/3 "2022-12-20T08:40:02Z")

</div>

Hello,

You can save your result as JSON and import it with APOC: apoc.load.json ([https://neo4j.com/labs/apoc/4.4/overview/apoc.load/apoc.load.json/](https://neo4j.com/labs/apoc/4.4/overview/apoc.load/apoc.load.json/))

Not tested, but query could look like this:

```auto
CALL apoc.load.json("path/to/file.json") YIELD value
MERGE (head:Entity {name: value.head})
MERGE (tail:Entity {name: value.tail})
CALL apoc.create.relationship(head, value.type, value.meta, tail) YIELD rel
RETURN count(rel)

```

Explantions:

- "get or create" the head entity whose name is the head value is the JSON
- Same for the tail entity  
**NOTE** : if entities are already in the database, just use MATCH instead of MERGE
- Then, create the relationship between entities, using apoc.create.relationship to create a relationship with dynamic type ([https://neo4j.com/labs/apoc/4.4/overview/apoc.create/apoc.create.relationship/](https://neo4j.com/labs/apoc/4.4/overview/apoc.create/apoc.create.relationship/))
- In the end, just return the number of created relationships. Up to you to decide what you want to see in the end, if you want to see something

---

<div class="post-metadata">

**Author:** ![Estelle](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/estelle/32/33070_2.png) [@Estelle](https://community.neo4j.com/u/Estelle)\
**Post date:** [December 20, 2022, 11:05am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/4 "2022-12-20T11:05:48Z")

</div>

Sure you can also do that. In your KB class you can probably have a method returning dictionnaries, then the dict keys are used as variables in the query you execute with the driver, something like:

```auto
query = """
MERGE (head:Entity {name: $head})
MERGE (tail:Entity {name: $tail})
CALL apoc.create.relationship(head, $type, $meta, tail) YIELD rel
RETURN count(rel)
"""
params = {
   "head": "",
   "type": "",
   "tail": "",
   "meta": {}
}
with driver.session() as s:
   s.run(query, params)

```

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 20, 2022, 11:04am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/5 "2022-12-20T11:04:15Z")

</div>

![Jupyter123_0-1671534233599.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/e/9/e99bc60e3970038519ef9e675eec6992e15793be.png "Jupyter123\_0-1671534233599.png")

[@Estelle](https://community.neo4j.com/t5/user/viewprofilepage/user-id/6250) Hi, im getting this error.

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 21, 2022, 3:42pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/6 "2022-12-21T15:42:30Z")

</div>

[@glilienfield](https://community.neo4j.com/t5/user/viewprofilepage/user-id/9010) i saw your graph, it's close to my expected output.  
just one quick change, instead of displaying node as numbers, can you display as corresponding to head and tail name in json?

something like this

![Jupyter123_0-1671637337614.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/c/3/c35a11b18caab05d05328f1d34033b4a7a01a916.png "Jupyter123\_0-1671637337614.png")

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [December 21, 2022, 3:22pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/7 "2022-12-21T15:22:06Z")

</div>

The json file consists of a single json object with key 'relations', whose value is an array. As such, [@Estelle](https://community.neo4j.com/t5/user/viewprofilepage/user-id/6250) query need to be modified to account for that. Try the following:

```auto
CALL apoc.load.json("file:///sample.json") YIELD value
UNWIND value.relations as relation
MERGE (head:Entity {name: relation.head})
MERGE (tail:Entity {name: relation.tail})
WITH head, relation, tail, toUpper(replace(relation.type, " ", "_")) as type
CALL apoc.create.relationship(head, type, {}, tail) YIELD rel
RETURN count(rel)

```

I replaced the relationship values to conform to best practices. Also, the 'meta' value is a json map, which can not be saved as a relationship property. Is there data in 'meta' that you want to save?

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 21, 2022, 3:30pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/8 "2022-12-21T15:30:22Z")

</div>

hi there [@glilienfield](https://community.neo4j.com/t5/user/viewprofilepage/user-id/9010) [@Estelle](https://community.neo4j.com/t5/user/viewprofilepage/user-id/6250)  
i dont want the meta value

i just want the 'head' node, 'tail' node and 'type' relation and that should form a graph.

by the way, i executed your code and i get like this.  
please see below image

![Jupyter123_0-1671636647305.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/0/8/083828aa1113b3113ba9e0bdf4cf9e94ad241488.png "Jupyter123\_0-1671636647305.png")

still i haven't got any graph though.

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [December 21, 2022, 3:45pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/9 "2022-12-21T15:45:48Z")

</div>

Interesting. I got 23 relationships. What does the output look like when you execute:

```auto
CALL apoc.load.json("file:///sample.json") YIELD value
return value

```

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [December 21, 2022, 3:34pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/10 "2022-12-21T15:34:34Z")

</div>

You did not attach your image. This is what I got:

![Screen Shot 2022-12-21 at 10.33.51 AM.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/6/860448bbd8d182790005593d670f4c26b49db11c.png "Screen Shot 2022-12-21 at 10.33.51 AM.png")

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [December 21, 2022, 3:00pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/11 "2022-12-21T15:00:50Z")

</div>

Its because you have an extra 'with' clause at the end of the first line. You need to put the 'with' between the 'merge' and 'call' on line 3.

```auto
WITH head, value, tail

```

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 21, 2022, 4:33am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/12 "2022-12-21T04:33:59Z")

</div>

[@Estelle](https://community.neo4j.com/t5/user/viewprofilepage/user-id/6250) hi there, the query seems to be showing many errors like...

in-between merge and call 'with' should come.

could you please test the query and let me know

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 21, 2022, 3:51pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/13 "2022-12-21T15:51:12Z")

</div>

[@glilienfield](https://community.neo4j.com/t5/user/viewprofilepage/user-id/9010) the output look like this

![Jupyter123_0-1671637863033.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/4/a/4afddb0e8e08f90fd5a601f70976ff808fd80ccf.png "Jupyter123\_0-1671637863033.png")

i dont see any nodes getting created

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 21, 2022, 3:36pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/14 "2022-12-21T15:36:14Z")

</div>

i edited it, pls check the image and it's nice you have got my expected graph.

Could you check what's the error i did tho?

---

<div class="post-metadata">

**Author:** ![Estelle](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/estelle/32/33070_2.png) [@Estelle](https://community.neo4j.com/u/Estelle)\
**Post date:** [December 21, 2022, 7:59am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/15 "2022-12-21T07:59:46Z")

</div>

Hi [@Jupyter123](https://community.neo4j.com/t5/user/viewprofilepage/user-id/16111) ,

The error message seems quite explicit, what have you tried to fix the query and what is your problem?

---

<div class="post-metadata">

**Author:** ![glilienfield](https://sea1.discourse-cdn.com/flex021/user_avatar/community.neo4j.com/glilienfield/32/27534_2.png) [@glilienfield](https://community.neo4j.com/u/glilienfield)\
**Post date:** [December 21, 2022, 4:54pm UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/16 "2022-12-21T16:54:19Z")

</div>

Ok, that structure is different from what you posted. That data looks to be an array. It is not a value of the 'relation' key, as was in the file you posted. That would explain the issue. Can you post the final json file? I can modify the query.

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 21, 2022, 10:50am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/17 "2022-12-21T10:50:45Z")

</div>

hey [@Estelle](https://community.neo4j.com/t5/user/viewprofilepage/user-id/6250)

I have uploaded JSON file in the below link. Please have a look

[https://easyupload.io/y7ne07](https://easyupload.io/y7ne07)

I want that JSON data to be displayed in neo4j graph DB with nodes and edges

What is the query for that?

Could you try to test that JSON file in neo4j and let me know if it's displaying in neo4j as graph?

I want the result to be like this

![Jupyter123_0-1671619816743.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/7/6/76e32e802d6ab40b09ef9e8473759465cd512f37.png "Jupyter123\_0-1671619816743.png")

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 22, 2022, 4:03am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/18 "2022-12-22T04:03:10Z")

</div>

[@glilienfield](https://community.neo4j.com/t5/user/viewprofilepage/user-id/9010)

hey mate, its working now

my bad , earlier i had wrong JSON file.

Now i updated json and it works.

![Jupyter123_0-1671681774130.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/8/d/8d5da645585f11f8970e4712b26839d6da785b52.png "Jupyter123\_0-1671681774130.png")

thank you very much

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 22, 2022, 3:50am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/19 "2022-12-22T03:50:50Z")

</div>

hi [@glilienfield](https://community.neo4j.com/t5/user/viewprofilepage/user-id/9010) , morning.

oh yeah i just checked, i used different wrong json structure.

let me use the one which i sent you and will update you

---

<div class="post-metadata">

**Author:** ![Jupyter123](https://avatars.discourse-cdn.com/v4/letter/j/8baadc/32.png) [@Jupyter123](https://community.neo4j.com/u/Jupyter123)\
**Post date:** [December 26, 2022, 10:25am UTC](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779/20 "2022-12-26T10:25:50Z")

</div>

hey [@glilienfield](https://community.neo4j.com/t5/user/viewprofilepage/user-id/9010)

just a small help

can you modify the same query in such a way where there should be a top node among all these nodes and that top node should connect every head node.

top node should be named as 'main' and that 'main' top node should relate with every other head node .

pls note that: top node is not in json file.

json file has head node, tail node and type(relation)

i want to create a single top node and that top node should match all head nodes..

what's the query for that?

something like this

![Jupyter123_0-1672050735285.png](https://us1.discourse-cdn.com/flex021/uploads/neo4jcommunity/original/3X/0/2/0216d2d32dca94dacb12b699fb141152b79a0725.png "Jupyter123\_0-1672050735285.png")

[Next page](https://community.neo4j.com/t/how-to-form-a-graph-db-from-relation-object/58779.md?page=2)
