apoc.map.submap

Function APOC Core

apoc.map.submap(map,keys,[defaults],[fail=true]) - returns submap for keys or throws exception if one of the key doesn’t exist and no default value given at that position

Signature

apoc.map.submap(map :: MAP?, keys :: LIST? OF STRING?, values = [] :: LIST? OF ANY?, fail = true :: BOOLEAN?) :: (MAP?)

Input parameters

Name Type Default

map

MAP?

null

keys

LIST? OF STRING?

null

values

LIST? OF ANY?

[]

fail

BOOLEAN?

true

Usage Examples

The following returns a map containing only the key a:

RETURN apoc.map.submap({a:1,b:1},['a']) AS output;
Table 1. 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;
Table 2. 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;
Table 3. 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;
Table 4. Results
Output

{c: NULL}