Home > database >  Flat an multiply arrays in Nifi
Flat an multiply arrays in Nifi

Time:10-22

I have the following JSON input:

{
  "id": 9256123,
  "data": [
    {
      "id": 242424,
      "attributes": [
        {
          "currency": "EUR",
          "price": "99,99"
        }
      ]
    },
    {
      "id": 22222,
      "attributes": [
        {
          "currency": "EUR",
          "price": "299,99"
        }
      ]
    }
  ]
}

I need to remove/flat the array

The output I need:

{
  "id": 9256123,
  "data": [
    {
      "id": 242424,
      "currency": "EUR",
      "price": "99,99"
    },
    {
      "id": 22222,
      "currency": "EUR",
      "price": "299,99"
    }
  ]
}

How can I do this with the Jolt-Processor? Or maybe with the ReplaceText-Processor?

CodePudding user response:

You can walk by the attributes array with a shift transformation within a JoltTransformJSON processor such as

[
  {
    "operation": "shift",
    "spec": {
      "*": "&",
      "data": {
        "*": {
          "attributes": {
            "*": {
              "@": "&4[&3]", // go four level up the tree and grab "data", and walk by indexes of it to return array of objects resukt by using "[&3]" 
              "@(2,id)": "&4[&3].id"
            }
          }
        }
      }
    }
  }
]

the demo on the site enter image description here

  • Related