takotsubo
takotsubo

Reputation: 746

JOLT to divide number inside array

I have JSON:

{
  "queryParams": {
    "limit": [
      "500"
    ],
    "breakdowns": [
      "impression_device,publisher_platform,platform_position"
    ]
  },
  "body": ""
}

I want to dynamically divide number inside limit array by 2.

I tried with:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "queryParams": {
        "limit": "=divide(@(2,limit), 2)"
      }
    }
  }
]

But it didnt change anything. How to fix it?

Expected:

{
  "queryParams": {
    "limit": [
      "250"
    ],
    "breakdowns": [
      "impression_device,publisher_platform,platform_position"
    ]
  },
  "body": ""
}

Upvotes: 1

Views: 354

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65323

Need two step conversion, since limit is not an integer or a single value but an array, and need to extract the desired value from that array by using some functions such as firstElement, intSum .. etc. in the first step, and then apply divide within modify-overwrite-beta transformation. If you want to hide the auxiliary key(lmt), then apply shift transformation consecutively.

So, consider using the specification below

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "queryParams": {
        "lmt": "=firstElement(@(1,limit))",
        "limit": "=divide(@(1,lmt),2)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "queryParams": {
        "limit": "&1.limit",
        "breakdowns": "&1.breakdowns"        
      },
      "body": "body"
    }
  }
]

Upvotes: 1

Related Questions