Merge similar JSON messages into one using jolt

Viewed 30

some help required for a jolt transformation that I am trying to achieve. Mainly I need to retain the key_id and contacts element and then group all elements in contacts into an array. This to merge multiple JSON messages into one batch json group I have this input

[
  {
    "key_id": "436",
    "contacts": [
      {
        "436": "aaaa-10f2-4cc3-820c-444444444",
        "1378": "2021-11-08",
        "1381": "2",
        "1421": "1"
      }
    ]
  },
  {
    "key_id": "436",
    "contacts": [
      {
        "436": "1111111111-e213-4c90-a8e8-6666gtggggf",
        "1378": "2021-11-09",
        "1381": "2",
        "1421": "1"
      }
    ]
  },
  {
    "key_id": "436",
    "contacts": [
      {
        "436": "xxxxxxxx-e213-4c90-a8e8-xxxxxxxxxxx",
        "1378": "2021-11-05",
        "1381": "2",
        "1421": "1"
      }
    ]
  }
]

And would like to transform it with jolt into something like this


[
  {
    "key_id": "436",
    "contacts": [
      {
        "436": "aaaa-10f2-4cc3-820c-444444444",
        "1378": "2021-11-08",
        "1381": "2",
        "1421": "1"
      },
      {
        "436": "1111111111-e213-4c90-a8e8-6666gtggggf",
        "1378": "2021-11-09",
        "1381": "2",
        "1421": "1"
      },
      {
        "436": "xxxxxxxx-e213-4c90-a8e8-xxxxxxxxxxx",
        "1378": "2021-11-05",
        "1381": "2",
        "1421": "1"
      }
    ]
  }
]

I've tried multiple transformations, but no luck so far. Thanks in advance

1 Answers

You can use shift transformation spec in order to combine the sub-objects of the contacts array respectively while retain all identical elements of key_id, and then pick leftmost key_id through use of cardinality transformation such as

[
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": "[0].&",
        "contacts": {
          "*": "[0].&1"
        }
      }
    }
  },
  {
    "operation": "cardinality",
    "spec": {
      "*": {
        "key_id": "ONE"
      }
    }
  }
]

enter image description here

Related