$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

FieldDescriptionRequired

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

{
    "$buildMap": [
        {
            "condition": {
                "$.request.queryParams.name": {
                    "$neq": null
                }
            },
            "key": "name",
            "value": "$.request.queryParams.name"
        },
        {
            "condition": {
                "$.request.queryParams.jobTitle": {
                    "$neq": null
                }
            },
            "key": "jobTitle",
            "value": "$.request.queryParams.jobTitle"
        },
        {
            "condition": {
                "$.request.queryParams.age": {
                    "$ge": 10
                }
            },
            "key": "age",
            "value": "$.request.queryParams.age"
        }
    ]
}

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

{
    "name": "$.request.queryParams.name",
    "jobTitle": "$.request.queryParams.jobTitle"
}

Last updated