# $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

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

### Properties

<table><thead><tr><th>Field</th><th width="332">Description</th><th>Required</th></tr></thead><tbody><tr><td>condition</td><td><p>The <code>&#x3C;condition></code> can be any valid <a href="../building-conditions"><mark style="color:blue;"><strong>condition object</strong></mark></a> as long as it resolves to a boolean object.</p><p></p><p>It can also be a <a href="../magical-autocomplete">Magical Autocomplete</a>.</p></td><td>true</td></tr><tr><td>key</td><td>The Key of the Key-Value pair to be inserted, if the condition holds true.</td><td>true</td></tr><tr><td>value</td><td>The Value of the Key-Value pair to be inserted, if the condition holds true.</td><td>true</td></tr></tbody></table>

### Returns

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

## Examples

### Basic example

```json
{
    "$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 -

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