# Creating Dynamic Queries

## Method 1: Using $buildMap Operator

The $buildMap operator is a dynamic JSON object builder that allows for conditional key-value pair insertion based on specified conditions. This is particularly useful for creating query objects for databases or APIs where certain parameters may be optional.

### Step-by-Step Guide

* **Define the Conditions** : Identify the conditions under which you want certain key-value pairs to be included in the query object. For instance, you might want to include a key only if a certain query parameter is present and meets specific criteria.
* **Create the $buildMap JSON Object**: Use the `$buildMap` operator to define your conditions, keys, and values.

### Example

Let's create a query object that includes name, jobTitle, and age based on the presence and values of these parameters in the request. &#x20;

```typescript
{
  $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',
      },
    },
  ],
};
```

### Explanation

* The condition field contains a JSON object that defines the condition.
* &#x20;The key field specifies the key to be inserted in the query object if the condition is met.
* &#x20; The value field specifies the value to be assigned to the key.

### Example 1

Assuming `name` and `jobTitle` are not null and \`age\` is less than 10, the resulting JSON object will be:

<pre class="language-typescript"><code class="lang-typescript">{
  "name": "$.request.queryParams.name",
<strong>  "jobTitle": "$.request.queryParams.jobTitle"
</strong>}
</code></pre>

Note: The `age` key gets omitted as the condition we had specified was to include the key when age is greater than or equal to 10.

### Example 2

Now, let's assume that \`name\` is not being passed as a query parameter and therefore is `null`. We pass `jobTitle` as intern and `age` as 24.

The resulting JSON object will be:

<pre class="language-typescript"><code class="lang-typescript">{
<strong>  "jobTitle": "intern",
</strong>  "age": 24
}
</code></pre>

<br>

To know more about the **$buildMap** operator [click here](/flow-builder/cql-cosmocloud-query-language/building-expressions/usdbuildmap.md).

## Method 2: Using the buildMap Node

The buildMap node is used to create a new JSON object with optional key-value pairs based on specified conditions. This method is similar to the $buildMap operator but is implemented without using the operator.

### Step-by-Step Guide

1. Add a buildMap Node: Select the buildMap node to add it in your workflow.
2. &#x20;Configure the Node:
   1. &#x20;**Variable Name** : Specify the name of the JSON variable that will be created.
   2. **List of BuildMap Objects**: Click on Edit and define the list of buildmap objects, each containing a condition, key, and value.

### Example

Here's how you might configure the buildMap node to achieve the same result as the $buildMap operator example:

```typescript
 [
  {
    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',
  },
];
```

To know more about the **buildMap** node [click here](/flow-builder/node-types/variable-nodes/special/build-map.md).

\ <br>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.cosmocloud.io/examples-how-to/creating-dynamic-queries.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
