Home > Back-end >  Jolt Transformation - getting last element by field Sequence
Jolt Transformation - getting last element by field Sequence

Time:08-24

Say I have the following Json:

{
  "collection": [
    {
      "sequence": 1,
      "value": "event1"
    },
    {
      "sequence": 2,
      "value": "event2"
    },
    {
      "sequence": 3,
      "value": "event3"
    }
  ]
}

Given that the array is not guaranteed to be sorted by "sequence", how do I extract the element that has the highest value of "sequence"?

CodePudding user response:

the sub-objects of the "collection" array might be sorted by the sequence values by putting them as indexes for reformed array (call "objectWithHighestSequence") such as

[
  {
    "operation": "shift",
    "spec": {
      "collection": {
        "*": "objectWithHighestSequence[@(0,sequence)]"
      }
    }
  },
  {
   // only pick the last object after sorting within the shift transformation
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=lastElement(@(1,objectWithHighestSequence))"
    }
  }
]

the demo on the site enter image description here

  • Related