Search code examples
jsonapache-nifijolt

Array for one element in Array of json, Nifi


I have the following input json:

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

And I am using the below spec expression :

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

Giving me this kind of output:

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

But, If the input (array of jsons) has only one json, for example:

{
  "ListArray": [
    {
      "address": "address1",
      "contact": "123456789",
      "bpId": 123
    }
  ]
}

Then I am getting the following output

{
  "Address_list": "address1",
  "Contact_list": "123456789",
  "Id_list": 123
}

For one element, an array isn't forming. I want the output to be in an array even if there is only one element. Thanks, in advance.


Solution

  • Just a slight change, which is appending .[] to the currently existing ampersand(&), will resolve this such as

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