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.

{
  $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.

  • The key field specifies the key to be inserted in the query object if the condition is met.

  • 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:

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

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:

{
  "jobTitle": "intern",
  "age": 24
}

To know more about the $buildMap operator click here.

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. Configure the Node:

    1. 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:

 [
  {
    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.

Last updated