Create dynamic Json with neo4j path nodes

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

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
1 Like

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

1 Like

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>> 

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.

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

Can you try with the things I mentioned and let us know if you ran into any concrete questions.

1 Like

Hi @michael.hunger,

Thank you for all your help.
i used some different approch with your inputs and i got the desired output as i want.

i tried with your inputs but it was not giving the output as i want.

Could you please look into below question for would be very helpful for me .

Thank you

Great thread. I'm working with Neo4j embedded and I'm looking for an example of call the equivalent of "apoc.convert.toJson(path)" from Java directly (not from Cypher). I have include the Maven dependency below but I can't figure out the syntax.


I've tried the following but it returns an infinite recursion:

apoc.convert.Json converter = new apoc.convert.Json();
response = converter.toJson(p.path);

Thanks in advance

Hi, is it possible to get only tree structure (only id-s, or some certain selected-values from children) ?
apoc.convert.toTree returns with the structure also all properties of the child nodes in the tree.

Hey @paul.are

you can do this using java code and JSON parser.
i did the same since i need only selected property from the json.

Thank you for reply. I was hoping there is a method to reduce the ammount of data transmitted to application on database side. As i understand, you suggest to transform responce from database to suitable format on application level - in my case it means transmitting lots of information that is not needed by application.

good idea can you create an APOC issue, we can add it as a config option to include/exclude certain properties

1 Like

Thanks for suggestion. Created issue: