cancel
Showing results for 
Search instead for 
Did you mean: 

Visualize json file with relationships

starz10de
Node Clone

I am new to neo4j and I wanted to visualize a json file with its elements relationships. However, I could only success to visualize one part of the file (doc_id=1) using:

CALL apoc.load.json("file:///data/test/forum_test.json")
YIELD value as val
UNWIND val.doc_id as docs
UNWIND val.categs as ctgs
UNWIND ctgs.desc as ctdes
MERGE(node0:docs {doc_nr : val.doc_id})
MERGE(node1:descrp {dess : ctdes.text, label:ctdes.des})
WITH node0, node1
CALL apoc.create.relationship(node0, 'has', {}, node1) YIELD rel
RETURN *

I assume using this commands the visulaized documents will be linked based on ctdes.text

2X_1_1ba46db022dba2638aece52a228511f6120554f8.png

{
  "doc_id": 0,
  "categs": [
    {
      "cat_id": 0,
      "desc": [
        {
          "text": "aaa",
          "des": "aa"
        },
       {
          "text": "bbb",
          "des": "bb"
        },
         {
          "text": "ccc",
          "des": "cc"
         }
  ]
  },
  {
      "cat_id": 1,
      "desc": [
        {
          "text": "ddd",
          "des": "dd"
        },
       {
          "text": "eee",
          "des": "ee"
        },
         {
          "text": "fff",
          "des": "ff"
         }
  ]
  }
	  ],
	  
"doc_id": 1,
  "categs": [
    {
      "cat_id": 0,
      "desc": [
        {
          "text": "mmm",
          "des": "mm"
        },
       {
          "text": "kkk",
          "des": "kk"
        },
         {
          "text": "lll",
          "des": "ll"
         }
  ]
  },
  {
      "cat_id": 1,
      "desc": [
        {
          "text": "nnn",
          "des": "nn"
        },
        {
          "text": "ddd",
          "des": "dd"
        },
         {
          "text": "zzz",
          "des": "zz"
         }
  ]
  }
	  ]  
}

fs

1 ACCEPTED SOLUTION

sorry for the misunderstanding, yes no need to Category, only linking through similar desc/text which is here "dd" see the attached figure of my solution.

View solution in original post

19 REPLIES 19

intouch_vivek
Graph Steward

Hi @starz10de,

UNWIND / FOREACH is used to traverse a list.
Try below code to get your defined graph. Are you sure you do not need category node?

CALL apoc.load.json("file:///data/test/forum_test.json")
YIELD value as val
MERGE(doc:Docs {doc_nr : val.doc_id})
Foreach (cate in val.categs | Foreach (d in cate.desc |
Merge (desc:Description{text:d.text, des:d.des}) Merge (doc)-[:HAS]-(desc) ))

Hi,

unfortunately it returns only

{
  "doc_nr": 1
}

I need the result as it is in the attached graph, however, including the doc_id=0 as well

Json file mentioned is not correct, duplicate id is not possible in a json file.

This is the reason code is considering last one.
You can test using
CALL apoc.load.json("file:///data/test/forum_test.json")
YIELD value
return value

I don't see any duplicate, in the json file there are "doc_id": 0 and "doc_id": 1
or I miss something ?

They both are in same {}

ah I see, Thanks a lot ! I will correct and try again

now I got both documents but I need to add the categs/desc as it is in the attached graph earlier

Send me the json file

I uploded as text file as I can't as json, I am new to forum and I don't know how to send it to you directly.
forum_test.txt (1.2 KB)

both documents should be linked by the same desc/"ddd" found in both. I did this with CSV file but not yet with json

Try below and let me know if this what require

CALL apoc.load.json("file:///forum_test")
YIELD value as val
MERGE(doc:Docs {doc_nr : val.doc_id})
Foreach (cate in val.categs | Merge (cat:Category {cat_id : cate.cat_id}) Merge (doc)-[:HAS]->(cat) Foreach (d in cate.desc |
Merge (desc:Description{text:d.text, des:d.des}) Merge (cat)-[:HAS]-(desc) ))

Unfortunately it doesn't work yet. However, after fixing the json structure issue, my first solution work fine (see the attached figure). Maybe it is not the optimal solution as it has many unwind.


When I tried your solution I just see the documents nodes without any relationships. I would like to check your solution as well in case you have other suggestions. Thanks for your effort

my code generates below . However this is not as your expectation then please try other logic as per your requirement

I didn't use match p=()-.... , can you update it to have same result as I had? the documents should be linked through the similar cat/desc which is here "dd"

Sorry I did not understand. Are you saying you do not need Category

sorry for the misunderstanding, yes no need to Category, only linking through similar desc/text which is here "dd" see the attached figure of my solution.

Then my previous query will work
CALL apoc.load.json("file:///data/test/forum_test.json")
YIELD value as val
MERGE(doc:Docs {doc_nr : val.doc_id})
Foreach (cate in val.categs | Foreach (d in cate.desc |
Merge (desc:Description{text:d.text, des:d.des}) Merge (doc)-[:HAS]-(desc) ))

yes, I tried it and it works so now we have two solutions

😄 enjoy 1-1 free solution

starz10de
Node Clone

how to merge specific number of documents using your approach, for example I want to consider only the first 100 documents. Also how to merge based on specific properties, e,g, desc.text='ccc', I could achieve this in my approach by, WITH and WHERE; e.g., with ctgs.desc.text ,
WHERE ctgs.desc.text='ccc'