$buildMap

$buildMap operator helps you in building a dynamic JSON object, where you can have conditional Key-Value pairs.

This is super useful when we need to optionally insert key-value pairs based on some conditions and do not want to build multiple If-Else blocks.

Syntax

{
    "$buildMap": [
        {
            "condition": <conditional_obj>,
            "key": <key_to_be_inserted>,
            "value": <value_to_be_inserted>
            }
        }
    ]
}

Properties

Field
Description
Required

condition

The <condition> can be any valid condition object as long as it resolves to a boolean object.

It can also be a Magical Autocomplete.

true

key

The Key of the Key-Value pair to be inserted, if the condition holds true.

true

value

The Value of the Key-Value pair to be inserted, if the condition holds true.

true

Returns

json object - The result of $buildMap operation, with Key-Value pairs.

Examples

Basic example

The above expression, assuming name & jobTitle is null but age is less than 10, returns an JSON object such as -

Last updated