Search code examples
jsonapache-nifijolt

nifi:jolt: tranformation needed for different names with array formation of each name


Here is my input and based one filenamepath we will be grouping and add those recpective files into single array for each group.

[
  {
    "filename": "FF/raw/first/raw_A/filenameA_20240212002113.DAT"
  },
  {
    "filename": "FF/raw/first/raw_A/filenameA__20240205150101.DAT"
  },
  {
    "filename": "FF/raw/first/raw_B/filenameB_20240212002113.DAT"
  },
  {
    "filename": "FF/raw/first/raw_B/filenameB_20240205150101.DAT"
  }
]

and output is required in below format:

[
  {
    "raw_A": [
      "filenameA_20240212002113.DAT",
      "filenameA_20240205150101.DAT"
    ]
  },
  {
    "raw_B": [
      "filenameB_20240212002113.DAT",
      "filenameB_20240205150101.DAT"
    ]
  }
]

Solution

  • You can use following shift transformation specs :

    [
      {
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "*/*/*/*/*": "&(0,4).&(0,5)"// extract the leaf node by using 5th asterisk, eg. use &(0,5)
                                          // and penultimate one by &(0,4)
                                          // after splitting the whole path by slashes
            }
          }
        }
      },
      { // convert objects into independent arrays
        "operation": "shift",
        "spec": {
          "*": {
            "*": {
              "$": "&2.[#2]"
            }
          }
        }
      },
      { // rearrange the JSON as desired
        "operation": "shift",
        "spec": {
          "*": {
            "*": "[#2].&1"
          }
        }
      }
    ]