Create dynamic Json with neo4j path nodes


(12kunal34) #1

Hi Everyone ,

I need some help from you guys.
i know that we can return json output from cypher queries.but my scenario is quite different
i have nodes in below format


now problm is that this path can have multiple EXP and Multiple HAR

i want to create json like this

PI Node Details,
HARVEST Node details,
Expression node detail,
Dna Node details

please help me to handle dynamic Exp and Har.

Thanks in advance

(Michael Hunger) #2

Can you see if apoc.convert.toTree helps:

match path = (a)- ->(b) with collect(path) as paths
call apoc.convert.toTree(paths) yield value
return value

(12kunal34) #3

Thanks michael.
It is working for me ..:slight_smile:
but now problem is i want to get some particular properties in this path like we have some BARCODE in each node that comes in this path .i want to get these barcodes only in json not all properties.
please help me in this

(12kunal34) #4

Hi Michael ,

i want some json that have data from another node as well
please find query below that return all the nodes and info i need to display in json .

match path = (a)-[*]->(c:z{BARCODE:'asdf'})-[*]->(b)
// get all nodes (departments) from path
with nodes(path) as deps
// unwind deps collection to individual departments
unwind deps as dep
// match workers and managers directly connected to dep nodes
Optional match (dep)-[:ASSOCIATED{PREDICATE:'abc'}]->(u:ENTITY)
with collect(dep) as d, collect(u) as u
with collect(d + u) as p
 call apoc.convert.toTree(p) yield value return value

in above i have a query with optional match, i want to show u's barcode as json attribute for the path
please help me in this i am getting error in above query

Type mismatch: expected List<Path> but was List<List<Node>> 

(Michael Hunger) #5

There are some apoc functions to create paths from nodes + rels.

  • apoc.path.create
  • apoc.path.slice
  • apoc.path.combine
  • apoc.path.elements

You will be able to see their signature in call"apoc.path")

Also there are apoc functions for creating JSON from custom documents (nested maps/lists).

esp. apoc.convert.toJson

Those should help you with what you want to do.

(12kunal34) #6

Hi michael,
Thanks for all your help.
my query regarding data lineage.
my data is in following tree structure.

A <-associated- B <-associated- C <-associated- D <-associated- E
D <-associated- F
D <-associated- G
C <-associated- K
C <-associated- L

I want to show this data in a nested json
i will give only one node info according to that info i want to get json from Top most to lower most node and it should contain all nested data and subgraph

Thank you in advance