apoc.graph.fromDocument
Procedure
apoc.graph.fromDocument(json ANY, config MAP<STRING, ANY>)
- generates a virtual sub-graph by extracting all of the NODE
and RELATIONSHIP
values from the data returned by the given JSON file.
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). |
Config parameters
The procedure support the following 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 |
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;
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]] |
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:
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:
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:
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:
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"}});