apoc.refactor.normalizeAsBoolean

Procedure APOC Core

apoc.refactor.normalizeAsBoolean(entity, propertyKey, true_values, false_values) normalize/convert a property to be boolean

Signature

apoc.refactor.normalizeAsBoolean(entity :: ANY?, propertyKey :: STRING?, true_values :: LIST? OF ANY?, false_values :: LIST? OF ANY?) :: VOID

Input parameters

Name Type Default

entity

ANY?

null

propertyKey

STRING?

null

true_values

LIST? OF ANY?

null

false_values

LIST? OF ANY?

null

Usage Examples

The examples in this section are based on the following sample graph:

CREATE (:Person {prop: 'Y', name:'A'}),
       (:Person {prop: 'Yes', name:'B'}),
       (:Person {prop: 'NO', name:'C'}),
       (:Person {prop: 'X', name:'D'}),
       (:Person {prop: true, name:'E'}),
       (:Person {prop: false, name:'F'});

We want to transform some properties into a boolean, Y, Yes into true and the properties NO into false. The other properties that don’t match these possibilities will be set as null. This includes BOOLEAN values as well.

The following normalizes all applicable boolean values for all nodes that have the prop property:
MATCH (n)
CALL apoc.refactor.normalizeAsBoolean(n,'prop',['Y','Yes'],['NO'])
WITH n
ORDER BY n.id
RETURN n.name AS name, n.prop AS prop;
Table 1. Results
name prop

"A"

true

"B"

true

"C"

false

"D"

null

"E"

null

"F"

null

If you want to keep BOOLEAN values as they are, include them in the parameters trueValues and falseValues:

MATCH (n)
CALL apoc.refactor.normalizeAsBoolean(n,'prop',['Y','Yes', true],['NO', false])
WITH n
ORDER BY n.id
RETURN n.name AS name, n.prop AS prop;
Table 2. Results
name prop

"A"

true

"B"

true

"C"

false

"D"

null

"E"

true

"F"

false