Search code examples
jsonapache-nifijolt

JOLT - Nested arrays sum


I am not able to find a solution for this transformation. My goal is to sum all parts and storing it into an array with the same length as operations.

Input data:

{
  "operations": [
    {
      "partOne": 10,
      "partTwo": 12.5,
      "partThree": 30.5
    },
    {
      "partOne": 25.5,
      "partTwo": 2,
      "partThree": 7.5
    }
  ]
}

Output data:

{
  "costs": [
    53, // operations[0].partOne + operations[0].partTwo + operations[0].partThree
    35 // operations[1].partOne + operations[1].partTwo + operations[1].partThree
  ]
}

EDIT: I was able to find this intermediate step, still missing the sum part.

[
  {
    "operation": "shift",
    "spec": {
      "operations": {
        "*": {
          "partOne": "costs[&1].[]",
          "partTwo": "costs[&1].[]",
          "partThree": "costs[&1].[]"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": {
        "costs": "=doubleSum(@(1,costs[&1]))"
      }
    }
  }
]

Also, if you could share some documentation about JOLT specification I would highly appreciate that. Could not find anything to get me started properly.


Solution

  • I was able to find the solution.

    Spec:

    [
      {
        "operation": "shift",
        "spec": {
          "operations": {
            "*": {
              "partOne": "costs[&1].[]",
              "partTwo": "costs[&1].[]",
              "partThree": "costs[&1].[]"
            }
          }
        }
      },
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "costs": {
            "*": "=doubleSum"
          }
        }
      }
    ]