Head's Up! These forums are read-only. All users and content have migrated. Please join us at community.neo4j.com.
04-28-2020 04:21 AM
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
{
"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
Solved! Go to Solution.
04-28-2020 10:01 AM
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.
04-28-2020 06:56 AM
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) ))
04-28-2020 07:36 AM
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
04-28-2020 08:17 AM
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
04-28-2020 08:25 AM
I don't see any duplicate, in the json file there are "doc_id": 0 and "doc_id": 1
or I miss something ?
04-28-2020 08:29 AM
They both are in same {}
04-28-2020 08:31 AM
ah I see, Thanks a lot ! I will correct and try again
04-28-2020 08:43 AM
now I got both documents but I need to add the categs/desc as it is in the attached graph earlier
04-28-2020 08:45 AM
Send me the json file
04-28-2020 08:55 AM
04-28-2020 09:38 AM
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) ))
04-28-2020 09:44 AM
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.
04-28-2020 09:49 AM
my code generates below . However this is not as your expectation then please try other logic as per your requirement
04-28-2020 09:51 AM
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"
04-28-2020 09:57 AM
Sorry I did not understand. Are you saying you do not need Category
04-28-2020 10:01 AM
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.
04-28-2020 10:12 AM
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) ))
04-28-2020 10:24 AM
yes, I tried it and it works so now we have two solutions
04-28-2020 10:30 AM
😄 enjoy 1-1 free solution
05-03-2020 07:58 AM
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'
All the sessions of the conference are now available online