apoc.convert.fromJsonList

Function APOC Core

apoc.convert.fromJsonList('[1,2,3]'[,'json-path', 'path-options'])

Signature

apoc.convert.fromJsonList(list :: STRING?, path =  :: STRING?, pathOptions = null :: LIST? OF STRING?) :: (LIST? OF ANY?)

Input parameters

Name Type Default

list

STRING?

null

path

STRING?

pathOptions

LIST? OF STRING?

[]

Usage Examples

The following converts a JSON list into a Cypher list:

RETURN apoc.convert.fromJsonList('[1,2,3]') AS output;
Table 1. 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;
Table 2. 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;
Table 3. 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;
Table 4. Results
output

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

Also, we could leverage the apoc.json.validate procedure (provided with APOC full) to handle incorrectly formatted JSONs by continuing the execution instead of causing it to fail. For example, the following query returns 0 instead of failing due to InputCoercionException: Numeric value (18446744062065078016838) out of range:

WITH '{
  "foo": ["1", {"bar": 18446744062065078016838}, {"baz":  18446744062065078016838} ],
  "baz": 7
}' as json
CALL { WITH json CALL apoc.json.validate(json) YIELD value RETURN collect(value) AS errs }
RETURN CASE errs WHEN [] THEN apoc.convert.fromJsonList(json, '$..baz', []) ELSE 0 END AS value

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

RETURN apoc.convert.fromJsonList('{"name": "Neo4j"}') AS output;
Table 5. 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.