'Jolt Transform - Filter Array Where Fields equal

In a Jolt transform Im trying to filter an array of objects by an id, if equal to an Id on a field outside of the array.

Here is my data

{
  "position": {
    "positionManagerId": "123"
  },
  "managers": [
    {
      "id": "123",
      "name": "John"
    },
    {
      "id": "456",
      "name": "Jack"
    }
  ]
}

Id like to grab the manager where managers.id == position.positonManagerId

My output would look like

{
  "managerId": "123",
  "managerName": "John"
}


Solution 1:[1]

You can apply shift transformation twice.

Combine key-value pairs under the common id values as key names within the first one so as to get a list for the searched id value( 123 in this case ) while the other pair(s) won't be lists.

Then use index values(0and1) within the second spec in order to eliminate the pair(s) without list value such as

[
  {
    "operation": "shift",
    "spec": {
      "position": {
        "*": {
          "$": "@(0)"
        }
      },
      "managers": {
        "*": {
          "@name": "@id"
        }
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "0": {
          "$1": "managerId"
        },
        "1": "managerName"
      }
    }
  }
]

enter image description here

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Barbaros Özhan