apoc.graph.fromDocument

This procedure returns virtual nodes and relationships that can only be accessed by other APOC procedures. For more information, see Virtual Nodes & Relationships (Graph Projections).
Details

Syntax

apoc.graph.fromDocument(json [, config ]) :: (graph)

Description

Generates a virtual sub-graph by extracting all of the NODE and RELATIONSHIP values from the data returned by the given JSON file.

Input arguments

Name

Type

Description

json

ANY

A JSON object to generate a graph from.

config

MAP

{ write = false :: BOOLEAN, labelField = 'type' :: STRING. idField = 'id' :: STRING, generateID = true :: BOOLEAN, defaultLabel = '' :: STRING, skipValidation = false :: BOOLEAN, mappings = {} :: MAP }. The default is: {}.

Return arguments

Name

Type

Description

graph

MAP

The resulting graph.

Config parameters

The procedure supports the following config parameters:

Config parameters
Name Type Default Description

write

BOOLEAN

false

Persist the graph otherwise return a Virtual Graph.

labelField

STRING

type

The field name that became the label of the node.

idField

STRING

id

The document field name that will become the id field of the created nodes (used for node resolution when you create relationships between nodes).

generateId

BOOLEAN

true

In case of missing id-field value it generates a UUID for it.

defaultLabel

STRING

""

In case of missing label-field value is uses the provided default label.

skipValidation

BOOLEAN

false

In case you want skip the validation process into the apoc.graph.fromDocument.

mappings

MAP<STRING, STRING>

{}

Click on link below for more detail.

Usage Examples

CALL apoc.graph.fromDocument("{'id': 1,'type': 'artist','name':'Genesis','members': ['Tony Banks','Mike Rutherford','Phil Collins'],'years': [1967, 1998, 1999, 2000, 2006],'albums': [{'type': 'album','id': 1,'producer': 'Jonathan King','title': 'From Genesis to Revelation'}]}", {write: false})
YIELD graph AS g
RETURN g.nodes AS nodes, g.relationships AS rels;
Results
nodes rels

[(:Artist {name: "Genesis", id: 1, type: "artist", years: [1967, 1998, 1999, 2000, 2006], members: ["Tony Banks", "Mike Rutherford", "Phil Collins"]}), (:Album {producer: "Jonathan King", id: 1, type: "album", title: "From Genesis to Revelation"})]

[[:ALBUMS]]

Virtual graph from JSON with labelField
CALL apoc.graph.fromDocument("{'id': 1,'type': 'artist','name':'Genesis','members': ['Tony Banks','Mike Rutherford','Phil Collins'],'years': [1967, 1998, 1999, 2000, 2006],'albums': [{'type': 'album','id': 1,'producer': 'Jonathan King','title': 'From Genesis to Revelation'}]}", {write: false})
YIELD graph
RETURN *

As a result we have a virtual graph with two nodes and one relationship:

apoc.graph.fromDocument 1
Virtual graph from JSON with labelField
CALL apoc.graph.fromDocument('{"id":10,"myCustomType":"labelArtist","name":"Genesis","albums":[{"myCustomType":"labelAlbum","producer":"Jonathan King","id":20,"title":"From Genesis to Revelation"}]}', {labelField: "myCustomType"})
YIELD graph
RETURN *

As a result we have a virtual graph with two nodes and one relationship:

apoc.graph.fromDocument with label type
Virtual graph from JSON with labelField and idField
CALL apoc.graph.fromDocument('{"myCustomType":"labelArtist","name":"Genesis","myCustomId":1,"albums":[{"myCustomType":"labelAlbum","producer":"Jonathan King","myCustomId":1,"title":"From Genesis to Revelation"}]}',
{labelField: "myCustomType", idField: "myCustomId"})
YIELD graph
RETURN *

As a result we have a virtual graph with two nodes and one relationship:

apoc.graph.fromDocument with label type and id field
Virtual graph from JSON with mappings
CALL apoc.graph.fromDocument('{"id":1,"type":"Person","name":"Andrea","sizes":{"weight":{"value":70,"um":"Kg"},"height":{"value":174,"um":"cm"},"array":["foo","bar"]},"books":[{"title":"Flow My Tears, the Policeman Said","released":1974},{"title":"The man in the High Castle","released":1962}]}',
{mappings:{`$`:"Person:Reader{*,@sizes}",`$.books`:"Book{!title, released}"}})
yield graph
RETURN *

As a result we have a virtual graph with three nodes and two relationship:

apoc.graph.fromDocument with mappings
Virtual graph from JSON with relMapping

We can pass a relMapping to customize relationship names, passing a map with the relationships you want to change as keys. For example:

CALL apoc.graph.fromDocument("{'id': 1,'type': 'artist','name':'Genesis','members': ['Tony Banks','Mike Rutherford','Phil Collins'],'years': [1967, 1998, 1999, 2000, 2006],'albums': [{'type': 'album','id': 1,'producer': 'Jonathan King','title': 'From Genesis to Revelation'}]}",
{relMapping: {albums: "CUSTOM_REL"}});
apoc.graph.fromDocument with relMapping