Return certain properties from apoc.convert.toTree

I have a graph of Restaurants that are located in different plazas

I'm using the following statement but it's returning all the properties from Plaza and Restaurant

MATCH r=(p:Plaza)-[:in_plaza]->(rs:Restaurant)
WITH COLLECT(r) AS rs
CALL apoc.convert.toTree(rs) yield value
RETURN value

How I can only return certain properties instead of the whole object property same as when i do
MATCH (p:Plaza)-[:in_plaza]->(rs:Restaurant) return p.name as plaza, rs.name as restaurant only returns these two properties

Do you know what version of APOC you are using? I couldn't find the documentation for apoc.convert.toTree

i'm using version 3.5.0.3, it's true i can't find the documentation either, but there are multiple discussions on stack overflow with examples using it. it's documented in 3.3 https://neo4j-contrib.github.io/neo4j-apoc-procedures/index33.html#_from_tojson
below is a snapshot of the results i'm getting.

I'm thinking that if there's a way to filter the properties before using this function

Ah ok, its possible that this will do it:

nodes: map of list, a key is the name of the label, value is a list of properties that can be included or excluded. e.g nodes:{LABEL:<[prop1, prop2]/[-prop1, -prop2]>}

github changeset

So:

MATCH r=(p:Plaza)-[:in_plaza]->(rs:Restaurant)
WITH COLLECT(r) AS rs
CALL apoc.convert.toTree(rs, {
    nodes: {Plaza: ['name'], Restaurant: ['name']}
}) yield value
RETURN value

might do it :slight_smile: no promises!

1 Like

Thanks, It worked, however, i had to pass the second a required second parameter as boolean (lowercase/uppercase relationship names)

MATCH r=(p:Plaza)-[:in_plaza]->(rs:Restaurant) WITH COLLECT(r) AS rs CALL apoc.convert.toTree(rs, true, { nodes: {Plaza: ['name'], Restaurant: ['name']} }) yield value RETURN value

1 Like

Hello @melgamal, I've fallen into the same need and applied your query that worked for me, thanks by the way for sharing. However in the response I still get the _type and _id params. Did you manage to remove it as well from the response. Thanks

so far no, That's something i'm looking for too. P.S. Thanks to @jsmccrumb for the solution, not to me :slight_smile:

1 Like