Search code examples
jsonapache-nifitransformationjolt

Jolt spec to remove element from array


For my input json, I need to write a Jolt spec that can remove the externalId field from the list of externalIds in order to avoid repeating this element in the list.

Input:

{
  "id": "id_1",
  "targetId": "targetId42",
  "externalId": "1extid",
  "attributes": {
    "uniqueInfo": {
      "externalIds": [
        "3extid",
        "2extid",
        "4extid",
        "1extid",
        "5extid"
      ]
    }
  }
}

Desired output:

{
  "id": "id_1",
  "targetId": "targetId42",
  "externalId": "1extid",
  "attributes": {
    "uniqueInfo": {
      "externalIds": [
        "3extid",
        "2extid",
        "4extid",
        "5extid"
      ]
    }
  }
}

Could someone please help with this query. Thanks.


Solution

  • You can consecutively use "$":"@(0)" tecnique in order to remove the array which is generated due to coincident values (1extid) of externalId within shift transformation specs such that

    [
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "externalId": "attributes.uniqueInfo.externalIds.@(0)", //exchange key-value pair
          "@(0,externalId)": "externalId", //multiplexing value to keep for later steps
          "attributes": {
            "uniqueInfo": {
              "externalIds": {
                "*": {
                  "@(4,externalId)": "&4.&3.&2.@(0)" //exchange key-value pairs
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "attributes": {
            "uniqueInfo": {
              "externalIds": {
                "*": {
                  "$": "&4.&3.&2.@(0)"//exchange key-value pairs again
                }
              }
            }
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "attributes": {
            "uniqueInfo": {
              "externalIds": {
                "*": {
                  "*": "&4.&3.&2[&]"
                }
              }
            }
          }
        }
      }
    ]