apoc.map.submap

Details

Syntax

apoc.map.submap(map, keys [, values, fail ])

Description

Returns a sub-map for the given keys. If one of the keys does not exist, or lacks a default value, this function will throw an exception.

Arguments

Name

Type

Description

map

MAP

The map to extract a submap from.

keys

LIST<STRING>

The list of keys to extract into a submap.

values

LIST<ANY>

The default values of the given keys. The default is: [].

fail

BOOLEAN

If a key is not present and no default is provided, it will either throw an exception if true, or return a null value. The default is: true.

Returns

MAP

Usage Examples

The following returns a MAP containing only the key a:

RETURN apoc.map.submap({a:1,b:1},['a']) AS output;
Results
Output

{a: 1}

The following throw an exception because the map doesn’t contain the key c:

RETURN apoc.map.submap({a:1,b:1},['c']) AS output;
Results

Failed to invoke function apoc.map.submap: Caused by: java.lang.IllegalArgumentException: Key c is not of one of the existing keys [a, b]

The following returns a MAP containing a default value of 42 for the missing key c:

RETURN apoc.map.submap({a:1,b:1},['c'], [42]) AS output;
Results
Output

{c: 42}

The following returns a MAP containing a null value for the missing key c:

RETURN apoc.map.submap({a:1,b:1},['c'], null, false) AS output;
Results
Output

{c: NULL}