apoc.refactor.normalizeAsBoolean

Details

Syntax

apoc.refactor.normalizeAsBoolean(entity, propertyKey, trueValues, falseValues)

Description

Refactors the given property to a BOOLEAN.

Input arguments

Name

Type

Description

entity

ANY

The node or relationship whose properties will be normalized to booleans.

propertyKey

STRING

The name of the property key to normalize.

trueValues

LIST<ANY>

The possible representations of true values.

falseValues

LIST<ANY>

The possible representations of false values.

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;
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;
Results
name prop

"A"

true

"B"

true

"C"

false

"D"

null

"E"

true

"F"

false