apoc.paths.toJsonTree

Despite its name, this procedure do not output json.

Details

Syntax

apoc.paths.toJsonTree(paths [, lowerCaseRels, config ]) :: (value)

Description

Creates a stream of nested documents representing the graph as a tree by traversing outgoing relationships.

Input arguments

Name

Type

Description

paths

LIST<PATH>

A list of paths to convert into a tree.

lowerCaseRels

BOOLEAN

Whether or not to convert relationship types to lower case. The default is: true.

config

MAP

{ nodes = {} :: MAP, rels = {} :: MAP, sortPaths = true :: BOOLEAN }. The default is: {}.

Return arguments

Name

Type

Description

value

MAP

The resulting tree.

Config parameters

The procedure support the following config parameters:

Config parameters
Name Type Default Description

nodes

MAP<STRING, LIST<STRING>>

{}

properties to include or exclude for each node label e.g. to include {Movie: ['title']} or to exclude {Movie: ['-title']}.

rels

MAP<STRING, LIST<STRING>>

{}

properties to include or exclude for each relationship type e.g. to include {ACTED_IN: ["roles"]} or to exclude {ACTED_IN: ["-roles"]}.

sortPaths

BOOLEAN

true

to sort the result by path length.

Usage examples

The examples in this section are based on the following sample graph:

CREATE (Keanu:Person {name:'Keanu Reeves', born:1964})
CREATE (TomH:Person {name:'Tom Hanks', born:1956})
CREATE (TomT:Person {name:'Tom Tykwer', born:1965})
CREATE (JamesThompson:Person {name:'James Thompson'})

CREATE (TheMatrix:Movie {title:'The Matrix', released:1999, tagline:'Welcome to the Real World'})
CREATE (TheMatrixReloaded:Movie {title:'The Matrix Reloaded', released:2003, tagline:'Free your mind'})
CREATE (TheMatrixRevolutions:Movie {title:'The Matrix Revolutions', released:2003, tagline:'Everything that has a beginning has an end'})
CREATE (SomethingsGottaGive:Movie {title:"Something's Gotta Give", released:2003})
CREATE (TheDevilsAdvocate:Movie {title:"The Devil's Advocate", released:1997, tagline:'Evil has its winning ways'})

CREATE (YouveGotMail:Movie {title:"You've Got Mail", released:1998, tagline:'At odds in life... in love on-line.'})
CREATE (SleeplessInSeattle:Movie {title:'Sleepless in Seattle', released:1993, tagline:'What if someone you never met, someone you never saw, someone you never knew was the only someone for you?'})
CREATE (ThatThingYouDo:Movie {title:'That Thing You Do', released:1996, tagline:'In every life there comes a time when that thing you dream becomes that thing you do'})
CREATE (CloudAtlas:Movie {title:'Cloud Atlas', released:2012, tagline:'Everything is connected'})

CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrix)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixReloaded)
CREATE (Keanu)-[:ACTED_IN {roles:['Neo']}]->(TheMatrixRevolutions)
CREATE (Keanu)-[:ACTED_IN {roles:['Julian Mercer']}]->(SomethingsGottaGive)
CREATE (Keanu)-[:ACTED_IN {roles:['Kevin Lomax']}]->(TheDevilsAdvocate)

CREATE (TomH)-[:ACTED_IN {roles:['Joe Fox']}]->(YouveGotMail)
CREATE (TomH)-[:ACTED_IN {roles:['Sam Baldwin']}]->(SleeplessInSeattle)
CREATE (TomH)-[:ACTED_IN {roles:['Mr. White']}]->(ThatThingYouDo)
CREATE (TomH)-[:ACTED_IN {roles:['Zachry', 'Dr. Henry Goose', 'Isaac Sachs', 'Dermot Hoggins']}]->(CloudAtlas)
CREATE (TomT)-[:DIRECTED]->(CloudAtlas)

CREATE (JamesThompson)-[:REVIEWED {summary:'Enjoyed it!', rating:95}]->(TheMatrix)
CREATE (JamesThompson)-[:REVIEWED {summary:'It was alright.', rating:65}]->(TheMatrixReloaded)
CREATE (JamesThompson)-[:REVIEWED {summary:'The best of the three', rating:100}]->(TheMatrixRevolutions);

The following converts a list of paths of Keanu Reeves movies into a nested map:

MATCH path = (p:Person {name: "Keanu Reeves"})-[:ACTED_IN]->(movie)
WITH collect(path) AS paths
CALL apoc.paths.toJsonTree(paths)
YIELD value
RETURN value;
Results
value
{
  _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:0",
  born: 1964,
  _type: "Person",
  name: "Keanu Reeves",
  acted_in: [
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:8",
      `acted_in._id`: 4,
      `acted_in.roles`: ["Kevin Lomax"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:4",
      _type: "Movie",
      tagline: "Evil has its winning ways",
      _id: 8,
      title: "The Devil's Advocate",
      released: 1997
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:7",
      `acted_in._id`: 3,
      `acted_in.roles`: ["Julian Mercer"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:3",
      _type: "Movie",
      _id: 7,
      title: "Something's Gotta Give", released: 2003
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:6",
      `acted_in._id`: 2,
      `acted_in.roles`: ["Neo"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:2",
      _type: "Movie",
      tagline: "Everything that has a beginning has an end",
      _id: 6,
      title: "The Matrix Revolutions",
      released: 2003
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:5",
      `acted_in._id`: 1,
      `acted_in.roles`: ["Neo"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:1",
      _type: "Movie",
      tagline: "Free your mind",
      _id: 5,
      title: "The Matrix Reloaded",
      released: 2003
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:4",
      `acted_in._id`: 0,
      `acted_in.roles`: ["Neo"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:0",
      _type: "Movie",
      tagline: "Welcome to the Real World",
      _id: 4,
      title: "The Matrix",
      released: 1999
    }
  ],
  _id: 0
}

By default, relationship types are converted to lower case. To keep the original case pass in false for the second parameter (lowerCaseRels):

MATCH path = (p:Person {name: "Keanu Reeves"})-[:ACTED_IN]->(movie)
WITH collect(path) AS paths
CALL apoc.paths.toJsonTree(paths, false)
YIELD value
RETURN value;
Results
value
{
  _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:0",
  ACTED_IN: [
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:8",
      `ACTED_IN._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:4",
      `ACTED_IN.roles`: ["Kevin Lomax"],
      _type: "Movie",
      tagline: "Evil has its winning ways",
      _id: 8,
      `ACTED_IN._id`: 4,
      title: "The Devil's Advocate",
      released: 1997
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:7",
      `ACTED_IN._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:3",
      `ACTED_IN.roles`: ["Julian Mercer"],
      _type: "Movie",
      _id: 7,
      `ACTED_IN._id`: 3,
      title: "Something's Gotta Give",
      released: 2003
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:6",
      `ACTED_IN._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:2",
      `ACTED_IN.roles`: ["Neo"],
      _type: "Movie",
      tagline: "Everything that has a beginning has an end",
      _id: 6,
      `ACTED_IN._id`: 2,
      title: "The Matrix Revolutions",
      released: 2003
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:5",
      `ACTED_IN._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:1",
      `ACTED_IN.roles`: ["Neo"],
      _type: "Movie",
      tagline: "Free your mind",
      _id: 5,
      `ACTED_IN._id`: 1,
      title: "The Matrix Reloaded",
      released: 2003
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:4",
      `ACTED_IN._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:0",
      `ACTED_IN.roles`: ["Neo"],
      _type: "Movie",
      tagline: "Welcome to the Real World",
      _id: 4,
      `ACTED_IN._id`: 0,
      title: "The Matrix",
      released: 1999
    }
  ],
  born: 1964,
  _type: "Person",
  name: "Keanu Reeves",
  _id: 0
}

By default, all properties are included for node labels and relationship types. To limit the properties for nodes use the nodes config key. For relationship types use the rels config key.

The following query demonstrates how to return only the title property for Movie nodes by setting the nodes config key:

MATCH path = (p:Person {name: "Keanu Reeves"})-[:ACTED_IN]->(movie)
WITH collect(path) AS paths
CALL apoc.paths.toJsonTree(paths, true, {
  nodes: {Movie: ['title']}
})
YIELD value
RETURN value;
Results
value
{
  _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:0",
  born: 1964,
  _type: "Person",
  name: "Keanu Reeves",
  acted_in: [
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:8",
      `acted_in._id`: 4,
      `acted_in.roles`: ["Kevin Lomax"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:4",
      _type: "Movie",
      _id: 8,
      title: "The Devil's Advocate"
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:7",
      `acted_in._id`: 3,
      `acted_in.roles`: ["Julian Mercer"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:3",
      _type: "Movie",
      _id: 7,
      title: "Something's Gotta Give"
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:6",
      `acted_in._id`: 2,
      `acted_in.roles`: ["Neo"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:2",
      _type: "Movie",
      _id: 6,
      title: "The Matrix Revolutions"
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:5",
      `acted_in._id`: 1,
      `acted_in.roles`: ["Neo"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:1",
      _type: "Movie",
      _id: 5,
      title: "The Matrix Reloaded"
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:4",
      `acted_in._id`: 0,
      `acted_in.roles`: ["Neo"],
      `acted_in._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:0",
      _type: "Movie",
      _id: 4,
      title: "The Matrix"
    }
  ],
  _id: 0
}

The following query demonstrates returning only the REVIEWED rating and title of Movie nodes reviewed by James Thompson:

MATCH path = (p:Person {name:'James Thompson'})-[:REVIEWED]->(movie)
WITH collect(path) AS paths
CALL apoc.paths.toJsonTree(paths, true, {
  nodes: {Movie: ['title']},
  rels:  {reviewed: ['rating']}
})
YIELD value
RETURN value;
Results
value
{
  _type: "Person",
  name: "James Thompson",
  _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:3",
  reviewed: [
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:4",
      `reviewed._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:10",
      _type: "Movie",
      _id: 4,
      `reviewed.rating`: 95,
      title: "The Matrix",
      `reviewed._id`: 10
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:5",
      `reviewed._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:11",
      _type: "Movie",
      _id: 5,
      `reviewed.rating`: 65,
      title: "The Matrix Reloaded",
      `reviewed._id`: 11
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:6",
      `reviewed._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:12",
      _type: "Movie",
      _id: 6,
      `reviewed.rating`: 100,
      title: "The Matrix Revolutions",
      `reviewed._id`: 12
    }
  ],
  _id: 3
}

It is also possible to explicitly exclude certain properties by prepending them with a minus symbol (-). The following query demonstrates how to exclude title properties on Movie nodes and rating properties on REVIEWED relationships.

MATCH path = (p:Person {name:'James Thompson'})-[:REVIEWED]->(movie)
WITH collect(path) AS paths
CALL apoc.paths.toJsonTree(paths, true, {
  nodes: {Movie: ['-title']},
  rels:  {reviewed: ['-rating']}
})
YIELD value
RETURN value;
Results
value
{
  _type: "Person",
  name: "James Thompson",
  _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:3",
  reviewed: [
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:4",
      `reviewed._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:10",
      _type: "Movie",
      tagline: "Welcome to the Real World",
      `reviewed.summary`: "Enjoyed it!",
      _id: 4,
      `reviewed._id`: 10,
      released: 1999
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:5",
      `reviewed._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:11",
      _type: "Movie",
      tagline: "Free your mind",
      `reviewed.summary`: "It was alright.",
      _id: 5,
      `reviewed._id`: 11,
      released: 2003
    },
    {
      _elementId: "4:53366983-cb5f-4f0d-b943-e336173d2547:6",
      `reviewed._elementId`: "5:53366983-cb5f-4f0d-b943-e336173d2547:12",
      _type: "Movie",
      tagline: "Everything that has a beginning has an end",
      `reviewed.summary`: "The best of the three",
      _id: 6,
      `reviewed._id`: 12,
      released: 2003
    }
  ],
  _id: 3
}