apoc.convert.fromYaml

Function Apoc Extended

apoc.convert.fromYaml(value, $config) - Deserializes the YAML string to Neo4j value

Signature

apoc.convert.fromYaml(value :: STRING, config = {} :: MAP) :: ANY

Input parameters

Name Type Default

value

STRING

null

config

MAP

{}

Config parameters

The procedure support the following config parameters:

Table 1. Config parameters
name type default description

disable

list of strings

empty list

To disable one or more configurations, enabled by default, of the library used under the hood. See here.

enable

list of strings

empty list

To enable one or more configurations of the library used under the hood. See here.

mapping

map of strings

empty map

to map complex YAMLs.

In order to read complex types not supported by FasterXML Jackson, like Long…​ we can use the mapping config to convert to the desired data type. For example, if we have a YAML a: 42 b: foo , we can define a map {mapping: {a: "Long"} }

Usage Examples

We can convert any YAML string to list/map of nodes/rels

Convert YAML string to map
RETURN apoc.convert.fromYaml("a: 42
b: foo") AS value
Table 2. Results
value
{
  "b": "foo",
  "a": 42
}
Convert YAML string to map, with custom mapping
RETURN apoc.convert.fromYaml("a: 42
b: foo", {mapping: {a: "Long"} }) AS value
Table 3. Results
value
{
  "b": "foo",
  "a": 42
}
Convert YAML string to map with custom features
RETURN apoc.convert.fromYaml("a: 42
b: foo", {enable: ['MINIMIZE_QUOTES'], disable: ['WRITE_DOC_START_MARKER']}) AS value
Table 4. Results
value
{
  "b": "foo",
  "a": 42
}
Convert YAML string to list
RETURN apoc.convert.fromYaml("---
- 1
- 2
- 3") as value
Table 5. Results
value
[1, 2, 3]
Convert YAML string to list and mapping type Long
RETURN apoc.convert.fromYaml("---
- 1
- 2
- 3",
{mapping: {_: "Long"}}) as value
Table 6. Results
value
[1, 2, 3]
Convert from YAML string to map
RETURN apoc.convert.fromYaml("---
                            a: 42
                            b: \"foo\"
                            c:
                            - 1
                            - 2
                            - 3") as value
Table 7. Results
value
{
  "b": "foo",
  "c": [
    1,
    2,
    3
  ],
  "a": 42
}
Convert from YAML string to node
RETURN apoc.convert.fromYaml("---
id: \"<elementID>\"
type: \"node\"
labels:
- \"Test\"
properties:
foo: 7") as value
Table 8. Results
value
{
    "id": "<elementID>",
    "labels": [
        "Test"
    ],
    "properties": {
        "foo": 7
    },
    "type": "node"
}
Convert from YAML string to map with null values
RETURN apoc.convert.fromYaml("---
a: null
b: \"myString\"
c:
- 1
- \"2\"
- null") as value
Table 9. Results
value
{
  "b": "myString",
  "c": [
    1,
    "2",
    null
  ],
  "a": null
}
Convert from YAML string to map of nodes
RETURN apoc.convert.fromYaml("---
one:
    id: \"8d3a6b87-39ad-4482-9ce7-5684fe79fc57\"
    type: \"node\"
    labels:
    - \"Test\"
    properties:
    foo: 7
two:
    id: \"3fc16aeb-629f-4181-97d2-a25b22b28b75\"
    type: \"node\"
    labels:
    - \"Test\"
    properties:
    bar: 9
") as value
Table 10. Results
value
{
    "two": {
        "id": "3fc16aeb-629f-4181-97d2-a25b22b28b75",
        "labels": [
            "Test"
        ],
        "properties": null,
        "bar": 9,
        "type": "node"
    },
    "one": {
        "id": "8d3a6b87-39ad-4482-9ce7-5684fe79fc57",
        "labels": [
            "Test"
        ],
        "foo": 7,
        "properties": null,
        "type": "node"
    }
}
Convert from YAML string to relationship
RETURN apoc.convert.fromYaml("---
id: \"94996be1-7200-48c2-81e8-479f28bba84d\"
type: \"relationship\"
label: \"KNOWS\"
start:
    id: \"8d3a6b87-39ad-4482-9ce7-5684fe79fc57\"
    type: \"node\"
    labels:
    - \"User\"
    properties:
    name: \"Adam\"
end:
    id: \"3fc16aeb-629f-4181-97d2-a25b22b28b75\"
    type: \"node\"
    labels:
    - \"User\"
    properties:
        name: \"Jim\"
        age: 42
properties:
    bffSince: \"P5M1DT12H\"
    since: 1993.1
") as value
Table 11. Results
value
{
  "id": "94996be1-7200-48c2-81e8-479f28bba84d",
  "start": {
    "id": "8d3a6b87-39ad-4482-9ce7-5684fe79fc57",
    "name": "Adam",
    "labels": [
      "User"
    ],
    "properties": null,
    "type": "node"
  },
  "label": "KNOWS",
  "properties": {
    "bffSince": "P5M1DT12H",
    "since": 1993.1
  },
  "type": "relationship",
  "end": {
    "id": "3fc16aeb-629f-4181-97d2-a25b22b28b75",
    "labels": [
      "User"
    ],
    "properties": {
      "name": "Jim",
      "age": 42
    },
    "type": "node"
  }
}