Search code examples
arraysjsonapache-nifijolt

How to get elements from array with Jolt Transform and make new attributes with their values in NiFi


I have a JSON:

{
  "FILE": "23, 21, 24, 25"
}

I need it to look like this:

[
  {
    "FILE": {
      "First": "23",
      "Second": "21",
      "Third": "24",
      "Fourth": "25"
    }
  }
]

I made an array first with jolt transformation:

[
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "FILE": "=split(',',@(1,FILE))"
    }
  }
]

So now I have:

{
  "FILE": [
    "23",
    "21",
    "24",
    "25"
  ]
}

How can I get elements from this array and make new attributes with them? Is there any way to get them by index maybe?


Solution

  • There's no converter function to determine literal ordinals, but suffixes (st,nd,rd,th) along with incremented numerical indexes might be presented through the following transformation :

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": "=split(', ',@(1,&))"
        }
      },
      {//generate values vs. keys
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "@": "&2.v_&",
              "$": "&2.k_&"
            }
          }
        }
      },
      {//increment keys by one
        "operation": "modify-overwrite-beta",
        "spec": {
          "*": {
            "k_*": "=intSum(1,@(1,&))"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "k_*|v_*": "&1.&(0,1)"
          }
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "0": "&2.@(1,[1])"
            }
          }
        }
      },
    ,
      { // add ordinal suffixes
        "operation": "shift",
        "spec": {
          "*": {
            "1|*1": {
              "@": "&2.&st"
            },
            "2|*2": {
              "@": "&2.&nd"
            },
            "3|*3": {
              "@": "&2.&rd"
            },
            "*|11|12|13": {
              "@": "&2.&th"
            }
          }
        }
      }
    ]
    

    which will result for a JSON input of :

    {
      "FILE" : {
        "1st" : "23",
        "2nd" : "21",
        "3rd" : "24",
        "4th" : "25",
        "5th" : "29",
        "6th" : "32"
      }
    }
    

    for the input

    {
      "FILE": "23, 21, 24, 25, 29, 32"
    }