Search code examples
jsonapache-nifijolt

Spec Expression for JOLT transform of Json Array


I have an input json which is of the form:

{
  "ListArray": [
    {
      "address": "address1",
      "contact": "123456789",
      "bpId": 123
    },
    {
      "address": "address2",
      "contact": "135792468",
      "bpId": 456
    },
    {
      "address": "address3",
      "contact": "246812356",
      "bpId": 678
    }
  ]
}

I want the output json to be in this format:

{
 "Address_list": ["address1","address2","address3"],
 "Contact_list": ["123456789","135792468","246812356"],
 "Id_list": [123,456,678]
}

Could someone tell me how should I do this. What should be jolt transformation expression for it


Solution

  • Using one step of shift type transformation is enough such as

    [
      {
        "operation": "shift",
        "spec": {
          "ListArray": {
            "*": {
              "address": "Address_list",
              "contact": "Contact_list",
              "bpId": "Id_list"
            }
          }
        }
      }
    ]
    

    If changing of the name of the keys was not the case, you'd directly use like this

    [
      {
        "operation": "shift",
        "spec": {
          "ListArray": {
            "*": {
              "*": "&"
            }
          }
        }
      }
    ]