bazaarvoice / jolt

JSON to JSON transformation library written in Java.
Apache License 2.0
1.56k stars 329 forks source link

Compute sum of values for repeating keys in a List #1168

Open gurveenbagga opened 2 years ago

gurveenbagga commented 2 years ago

We have a requirement to find the sum of a particular key type from a list. The type may repeat in the list i.e it can have multiple values in the list.

Here, we want to get the sum of amount field values from the returnItemAdjustments list with returnAdjustmentTypeId = 'RET_SALES_TAX_ADJ'.

Sample Input:

[
  {
    "returnId": "10051",
    "returnItemAdjustments": [
      {
        "returnItemSeqId": "00001",
        "amount": 28.64,
        "returnAdjustmentTypeId": "RET_SALES_TAX_ADJ",
        "description": "Return Sales Tax",
        "createdDate": null,
        "returnId": "10051",
        "returnAdjustmentId": "10051",
        "comments": "Return Sales Tax",
        "shipGroupSeqId": null
      },
      {
        "returnItemSeqId": "00001",
        "amount": 58.64,
        "returnAdjustmentTypeId": "RET_SALES_TAX_ADJ",
        "description": "Return Sales Tax",
        "createdDate": null,
        "returnId": "10051",
        "returnAdjustmentId": "10051",
        "comments": "Return Sales Tax",
        "shipGroupSeqId": null
      }
    ]
  },
  {
    "returnId": "10051",
    "returnItemAdjustments": [
      {
        "returnItemSeqId": "00002",
        "amount": 38.64,
        "returnAdjustmentTypeId": "RET_SALES_TAX_ADJ",
        "description": "Return Sales Tax",
        "createdDate": null,
        "returnId": "10051",
        "returnAdjustmentId": "10051",
        "comments": "Return Sales Tax",
        "shipGroupSeqId": null
      }
    ]
  }
]

Expected output:

[
  {
    "returnId": "10051",
   "returnTax": 87.28
  },
  {
    "returnId": "10051",
   "returnTax": 38.64
  }
]

Can some one help to identify the possible Jolt Spec for this problem statement?

ouxuyong commented 2 years ago

@gurveenbagga This spec of mine can achieve the effect you want, I hope it can help you. In addition, there is a jolt tutorial project, welcome to communicate and discuss jolt-universe.

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "returnId": "[#2].returnId",
        "returnItemAdjustments": {
          "*": {
            "amount": "[#4].returnTax[]"
          }
        }
      }
    }
  }, {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "returnTax": "=doubleSum(@(1,returnTax))"
      }
    }
  }
]
janvi04 commented 2 years ago

This spec can also help you get the desired result

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "returnItemAdjustments": {
          "*": {
            "returnAdjustmentTypeId": {
              "RET_SALES_TAX_ADJ": {
                "@(2,amount)": "[&5].returnSalesTaxAmount[]"
              }
            }
          }
        },
        "@": "[&]"
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "returnSalesTaxTotalAmount": "=doubleSum(@(1,returnSalesTaxAmount))"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "returnId": "[&1].returnId",
        "returnSalesTaxTotalAmount": "[&1].returnTax"
      }
    }
  }
]