Home > Software design >  Can we perform multiplication using indexes of the list in jolt?
Can we perform multiplication using indexes of the list in jolt?

Time:12-08

I'm trying to perform the multiplication of two different lists.

Is this possible to perform the multiplication corresponding indexes of the lists like list1[i] * list2[i].

Input JSON

{
  "salesTaxAmount": [
    5.2,
    5.2,
    5.2
  ],
  "quantity": [
    1,
    2,
    1
  ],
  "unitAmount": [
    20,
    20,
    20
  ]
}

Expected output

{
  "totalAmount":[20,40,20]
}

Here I want the totalAmount like quantity * unitAmount.

Could someone help me, please.

Thanks!

CodePudding user response:

There's no function such like multiply or product to be used within a modify spec but can be simulated by using divide function after reforming individual objects by a shift transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "q*|u*": {
        "*": "&.&1"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "prd_quantity": "=divide(1,@(1,quantity))",
        "Amt_": "=divide(@(1,unitAmount),@(1,prd_quantity))",
        "Amt": "=toInteger(@(1,Amt_))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "Amt": "totalAmount[]"
      }
    }
  }
]

the demo on the site enter image description here

  • Related