apoc.refactor.normalizeAsBoolean

Procedure

apoc.refactor.normalizeAsBoolean(entity ANY, propertyKey STRING, trueValues LIST<ANY>, falseValues LIST<ANY>) - refactors the given property to a BOOLEAN.

Signature

apoc.refactor.normalizeAsBoolean(entity :: ANY, propertyKey :: STRING, true_values :: LIST<ANY>, false_values :: LIST<ANY>)

Input parameters

Name Type Default

entity

ANY

null

propertyKey

STRING

null

true_values

LIST<ANY>

null

false_values

LIST<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