Search code examples
jsonjolt

How to Sum a Double value Jolt Transform v0.1.1


I am not able to find a solution for this transformation, i have a input JSON, model below, and i need to sum the values of "totalValue" tag.

Input JSON

{
  "id": "ec4f9fbd-f595-4d03-a514-2133aabdd138",
  "listOfOrderItem": [
    {
      "erpReferenceKey": null,
      "totalValue": 60.78,
      "discontValue": 0,
      "position": 5
    },
    {
      "erpReferenceKey": null,
      "totalValue": 28.66,
      "discontValue": 0,
      "position": 4
    },
    {
      "erpReferenceKey": null,
      "totalValue": 48.84,
      "discontValue": 0,
      "position": 3
    }
  ]
}

Expected output

{
  "Sum": 138,28
}

Solution

  • Your Sum above doesnt take in consideration all TotalValue from json and it should be 138.28. Assuming that is what you need, the following should work:

    [
      {
        "operation": "shift",
        "spec": {
          "listOfOrderItem": {
            "*": {
              "totalValue": "totalValues"
            }
          }
        }
      }
     ,
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "Sum": "=doubleSum(@(1,totalValues))"
        }
      },
      {
        "operation": "remove",
        "spec": {
          "totalValues": ""
        }
      }
    ]
    

    Hope that helps.