apoc.graph.fromDocument

Procedure APOC Core

apoc.graph.fromDocument({json}, {config}) yield graph - transform JSON documents into graph structures

Signature

apoc.graph.fromDocument(json :: ANY?, config = {} :: MAP?) :: (graph :: MAP?)

Input parameters

Name Type Default

json

ANY?

null

config

MAP?

{}

Config parameters

The procedure support the following config parameters:

Table 1. 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 an 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

Output parameters

Name Type

graph

MAP?

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;
Table 2. 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