ravibeli
ravibeli

Reputation: 494

Multiplication of values in Jolt Transformation Spec

I have input as shown below, If timeMeasurementUnitCode="DAY" then value I need to calculate as value*24*60, if timeMeasurementUnitCode="HOUR" then value value*60

I see there is one similar use case people are looking for, the ticket is opened on Jul 17, 2019: https://github.com/bazaarvoice/jolt/issues/832

Issue 832 seems still open now

Input:

{
  "freightCharacteristics": {
    "transitDuration": {
      "timeMeasurementUnitCode": "DAY",
      "value": 6
    },
    "loadingDuration": {
      "timeMeasurementUnitCode": "HOUR",
      "value": 6
    }
  }
}

Expected output: Based on the constant provided in the input as DAY or HOUR, I need to multiple no.of days*hours*minutes to arrive at my requirement as shown below.

{
  "transitDurationInMinutes" : 8640,
  "loadingDurationInMinutes" : 360
}

]

As per the suggestion from @mattyb, I used divide function to achieve what want to achieve by multiplication, logic is updated as below.

[
  {
    "operation": "shift",
    "spec": {
      "freightCharacteristics": {
        "transitDuration": {
          "timeMeasurementUnitCode": {
            "DAY": {
              "@(2,value)": "transitDurationInMinutes"
            },
            "*": {
              "@(2,value)": "transitDurationInMinutes"
            }
          }
        },
        "loadingDuration": {
          "timeMeasurementUnitCode": {
            "HOUR": {
              "@(2,value)": "loadingDurationInMinutes"
            },
            "*": {
              "@(2,value)": "loadingDurationInMinutes"
            }
          }
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "transitDurationInMinutes": "=divide(@(1,transitDurationInMinutes),0.00069444444)",
      "loadingDurationInMinutes": "=divide(@(1,loadingDurationInMinutes),0.01666666666)"
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "transitDurationInMinutes": "=toInteger",
      "loadingDurationInMinutes": "=toInteger"
    }
  }
]

Upvotes: 2

Views: 2250

Answers (1)

Barbaros Özhan
Barbaros Özhan

Reputation: 65323

Another option would be making DAY and HOUR literals object keys respectively in order to pick them conditionally within the next step such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "value": "@(1,timeMeasurementUnitCode).&"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "DAY": {
        "val": "=divide(1,@(1,value))",// 1/6 will be used to simulate a multiplication
        "div": "=divide(1440,@(1,val))",
        "transitDurationInMinutes": "=toInteger(@(1,div))"// to eliminate decimal representation which ends with ".0"
      },
      "HOUR": {
        "val": "=divide(1,@(1,value))",
        "div": "=divide(60,@(1,val))",
        "loadingDurationInMinutes": "=toInteger(@(1,div))"
      }
    }
  },
  {// get rid of object keys
    "operation": "shift",
    "spec": {
      "*": {
        "*Duration*": "&"
      }
    }
  }
]

the demo on the site http://jolt-demo.appspot.com/ is

enter image description here

Upvotes: 2

Related Questions