Search code examples
apache-nifijolt

Unable to convert group of fields from an array into respective individual arrays using JOLT transformation


Input:

{
  "chain": [
    {
      "formation": [
        {
          "itemID": "1111",
          "isConsiderable": false
        },
        {
          "itemID": "2222",
          "isConsiderable": false
        }
      ]
    },
    {
      "formation": [
        {
          "itemID": "3333",
          "isConsiderable": false
        },
        {
          "itemID": "4444",
          "isConsiderable": false
        }
      ]
    }
  ]
}

Jolt Spec:

    [
  {
    "operation": "shift",
    "spec": {
      "chain": {
        "*": {
          "formation": {
            "*": {
              "itemID": "productsList[&1].products"
            }
          }
        }
      }
    }
}
]

Current output:

    {
  "productsList" : [ {
    "products" : [ "1111", "3333" ]
  }, {
    "products" : [ "2222", "4444" ]
  } ]
}

Desired Output:

  {
    "productsList" : [ {
    "products" : [ "1111", "2222" ]
          }, {
    "products" : [ "3333", "4444" ]
     } ]
   }

I want to match itemID of each formation array and group them into an respective array. By using jolt transformation am able to group the products array but not as per the desired format and want to group the itemID of respective array but here grouping the fields happening on same index of all the arrays in the input json.

Thanks in Advance


Solution

  • In your spec &1 refers to the index in the array stored under the formation key. You need to specify the index in the chain array (&3) and then in the formation array - as in the spec below:

    [
      {
        "operation": "shift",
        "spec": {
          "chain": {
            "*": {
              "formation": {
                "*": {
                  "itemID": "productsList[&3].products[&1]"
                }
              }
            }
          }
        }
    }
    ]