apoc.convert.fromJsonList

Details

Syntax

apoc.convert.fromJsonList(list [, path, pathOptions ])

Description

Converts the given JSON list into a Cypher LIST<STRING>.

Arguments

Name

Type

Description

list

STRING

A JSON stringified list.

path

STRING

A JSON path expression used to extract a certain part from the list. The default is: ``.

pathOptions

LIST<STRING>

JSON path options: ('ALWAYS_RETURN_LIST', 'AS_PATH_LIST', 'DEFAULT_PATH_LEAF_TO_NULL', 'REQUIRE_PROPERTIES', 'SUPPRESS_EXCEPTIONS') The default is: null.

Returns

LIST<ANY>

Usage examples

The following converts a JSON list into a Cypher list:

RETURN apoc.convert.fromJsonList('[1,2,3]') AS output;
Results
Output

[1, 2, 3]

We can also use JSON path expressions to extract part of a JSON list. For example, the following extracts the name property from a JSON list of objects and returns a list of Cypher strings:

RETURN apoc.convert.fromJsonList('[
  {"name": "Neo4j"},
  {"name": "Graph Data Science Library"},
  {"name": "Bloom"}
]', '.name') AS output;
Results
Output

["Neo4j", "Graph Data Science Library", "Bloom"]

Moreover, we can customize the Json path options, adding as third parameter (pathOptions) a list of strings, where the strings are based on Enum<Option>. The default value is ["SUPPRESS_EXCEPTIONS", "DEFAULT_PATH_LEAF_TO_NULL"]. Note that we can also insert [], that is "without options". So we can execute (with default pathOptions):

RETURN apoc.convert.fromJsonList('{ "columns": {
      "col2": {
        "_id": "772col2"
      }
    }
}', '$..columns') AS output;
Results
output

[ {"col2": { "_id": "772col2" }}, null, null ]

or, with custom path options:

RETURN apoc.convert.fromJsonList('{ "columns": {
      "col2": {
        "_id": "772col2"
      }
    }
}', '$..columns', ['ALWAYS_RETURN_LIST']) AS output;
Results
output

[ {"col2": { "_id": "772col2" }} ]

If we try to convert a non-list structure, we’ll get an exception. For example:

RETURN apoc.convert.fromJsonList('{"name": "Neo4j"}') AS output;
Results

Failed to invoke function apoc.convert.fromJsonList: Caused by: com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of java.util.ArrayList<java.lang.Object> out of START_OBJECT token at [Source: (String)"{"name": "Neo4j"}"; line: 1, column: 1]

In this case we should instead use apoc.convert.fromJsonMap.