shwetha
shwetha

Reputation: 25

How can I add all the values in array with jolt?

input :

{
  "accounts": {
    "canara": 1,
    "sbi": 0,
    "axis": 1,
    "hdfc": 0
  }
}

expected output :

{
  "canara": 1,
  "sbi": 0,
  "axis": 1,
  "hdfc": 0,
  "total accounts": 2
}

I want the sum of all the accounts to be added in "total accounts". how can I achieve this with jolt?

Upvotes: 0

Views: 555

Answers (2)

Markin
Markin

Reputation: 66

I use this solution

[
 // sum all fields and create a new field
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "accounts": {
        "total accounts": "=intSum(@(1,canara),@(1,sbi),@(1,axis),@(1,hdfc))"
      }
    }
  },
  {
    //extract all field of the account
    "operation": "shift",
    "spec": {
      "accounts": {
        "*": "&"
      }
    }
  }
]

Upvotes: 0

kasptom
kasptom

Reputation: 2458

This spec should work:

[
  {
    "operation": "shift",
    "spec": {
      "accounts": {
        "*": [
          ".&",
          "accountsAccumulator"
        ]
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "total accounts": "=intSum(@(1,accountsAccumulator))"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "accountsAccumulator": null,
      "*": "&"
    }
  }
]

Have a look at:

Upvotes: 1

Related Questions