I want to create a graph from my recursive JSON file. My JSON data is given below.
"ChildNodes":[
{
"ChildNodes":[
{
"ChildNodes":[
{
"ChildNodes":[
{
"ChildNodes":[
{
"FSId":1,
"FileDetailId":1862,
"FileId":1225,
"FileName":"tables",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1861,
"FileId":1224,
"FileName":"Components",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":1885,
"FileId":1247,
"FileName":"Filters",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":1894,
"FileId":1253,
"FileName":"SFXPlugins",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":1899,
"FileId":1255,
"FileName":"SonicResources",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":1901,
"FileId":1257,
"FileName":"style",
"Type":[
"DI"
],
"collapsed":1
},
{
"ChildNodes":[
{
"ChildNodes":[
{
"ChildNodes":[
{
"FSId":1,
"FileDetailId":1905,
"FileId":1262,
"FileName":"Symphony",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1904,
"FileId":1260,
"FileName":"Symphony",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1903,
"FileId":1259,
"FileName":"NTSC",
"Type":[
"DI"
],
"collapsed":1
},
{
"ChildNodes":[
{
"ChildNodes":[
{
"FSId":1,
"FileDetailId":1910,
"FileId":1267,
"FileName":"Symphony",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1909,
"FileId":1265,
"FileName":"Symphony",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1908,
"FileId":1264,
"FileName":"PAL",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1902,
"FileId":1258,
"FileName":"Styles",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1860,
"FileId":1216,
"FileName":"CreateDisc",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":1913,
"FileId":1269,
"FileName":"de-DE",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":1994,
"FileId":1298,
"FileName":"en-US",
"Type":[
"DI"
],
"collapsed":1
},
{
"ChildNodes":[
{
"FSId":1,
"FileDetailId":2050,
"FileId":1328,
"FileName":"X02",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":2049,
"FileId":1327,
"FileName":"MCX",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":2066,
"FileId":1330,
"FileName":"MediaRenderer",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":2091,
"FileId":1335,
"FileName":"sk-SK",
"Type":[
"DI"
],
"collapsed":1
},
{
"ChildNodes":[
{
"FSId":1,
"FileDetailId":2108,
"FileId":1359,
"FileName":"de-DE",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":2116,
"FileId":1361,
"FileName":"en-US",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":2106,
"FileId":1350,
"FileName":"wow",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1856,
"FileId":1103,
"FileName":"ehome",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":2120,
"FileId":1363,
"FileName":"en",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":2122,
"FileId":1365,
"FileName":"en-US",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":2135,
"FileId":1376,
"FileName":"Fonts",
"Type":[
"DI"
],
"collapsed":1
},
{
"ChildNodes":[
{
"FSId":1,
"FileDetailId":3117,
"FileId":2927,
"FileName":"config",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":2956,
"FileId":2118,
"FileName":"System32",
"Type":[
"DI"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":1844,
"FileId":1051,
"FileName":"Windows",
"Type":[
"DI"
],
"collapsed":1
},
{
"FSId":1,
"FileDetailId":3813,
"FileId":3072,
"FileName":"$OrphanFiles",
"Type":[
"DI",
"IN"
],
"collapsed":1
}
],
"FSId":1,
"FileDetailId":0,
"FileId":5,
"FileName":"C:",
"Type":[
"DI"
],
"collapsed":1
}
I found solution in this link useful to me for building my graph - https://community.neo4j.com/t/import-nodes-from-recursively-structured-json/11704 I was able to build the graph with the help of above mentioned link. I used the query given below.
WITH 'file:///part-of-VTree.json' AS json_file
CALL apoc.load.json(json_file,"..FileId") YIELD value AS result
WITH json_file, result.result AS keys
UNWIND range(0,size(keys)-1) AS i
CALL apoc.load.json(json_file,"..[?(@.FileId == '" + toString(keys[i]) + "')].ChildNodes[*].FileId") YIELD value AS children
WITH keys[i] AS parent_key, children.result AS children
MERGE (parent:GroupNode {name: parent_key})
WITH parent, children
UNWIND children as child_key
MERGE (child:GroupNode {name: child_key})
MERGE (child)-[:CHILD_OF]->(parent)
RETURN *
This created a graph with nodes having "FileId" mentioned on it , as I was giving "FileId" as "name" property. When I change my query as below
WITH 'file:///part-of-VTree.json' AS json_file
CALL apoc.load.json(json_file,"..FileId") YIELD value AS result
WITH json_file, result.result AS keys
UNWIND range(0,size(keys)-1) AS i
CALL apoc.load.json(json_file,"..[?(@.FileId == '" + toString(keys[i]) + "')].ChildNodes[*].FileId") YIELD value AS children
WITH keys[i] AS parent_key, children.result AS children
MERGE (parent:GroupNode {Fid: parent_key})
WITH parent, children
UNWIND children as child_key
MERGE (child:GroupNode {Fid: child_key})
MERGE (child)-[:CHILD_OF]->(parent)
RETURN *
It gives me a graph with nodes which has nothing mentioned on it. Earlier I was getting nodes with "FileId" mentioned on it. what I want is nodes with "FileName" mentioned on it and "FileId" as its property.
With my current query I am not able to set properties for my node. Is there any way to set properties for my node and display "FileName" on nodes. "FileName" is not always unique so I can't create graph by extracting all "FileName" instead of "FileId". "FileId" is always unique in my case.
Can anyone help me in some way to get my desired result. I need some help. Thanks in advance!
I got it solved. This is the query I used :
WITH 'file:///part-of-VTree.json' AS json_file
CALL apoc.load.json(json_file,"$..FileId") YIELD value AS result
WITH json_file, result.result AS keys UNWIND range(0,size(keys)-1) AS i
CALL apoc.load.json(json_file,"$..[?(@.FileId == '" + toString(keys[i]) + "')].ChildNodes[*].FileId") YIELD value AS children
CALL apoc.load.json(json_file,"$..[?(@.FileId == '" + toString(keys[i]) + "')].FileName") YIELD value AS rname
CALL apoc.load.json(json_file,"$..[?(@.FileId == '" + toString(keys[i]) + "')].ChildNodes[*].FileName") YIELD value AS cname
WITH keys[i] AS parent_key, children.result AS children, rname.result AS rn, cname.result AS cn
MATCH (n {name:parent_key})
WITH n, children,rn,cn
UNWIND children AS child_key
OPTIONAL MATCH (n)-[:CHILD]->(m {name:child_key})
WITH n, rn, cn, collect(m) AS siblings
UNWIND range(0,size(rn)-1) AS k
SET n.label=toString(rn[k])
With cn,siblings
UNWIND range(0,size(cn)-1) AS j
FOREACH(j IN range(0, size(siblings)-1) |
FOREACH (p1 in [siblings[j]] |
Set p1.label=toString(cn[j])
))
RETURN *
Hope this would help someone.