prathik vijaykumar
prathik vijaykumar

Reputation: 395

Jolt transform to flatten a array in Nifi

I have the following input JSON :

{
  "a":[1,2,3,[4,5,6], [7], [8,9], 10, 11]
}

And I would like the output to be :

{
  "a":[1,2,3,4,5,6,7,8,9,10,11]
}

What can be jolt transform for this. Thanks!

Upvotes: 0

Views: 437

Answers (2)

Muhammad Atif Akram
Muhammad Atif Akram

Reputation: 1315

Try this If you need solution without using builtin function ! Obviously programming is what we solve the problems with logic without using packages and builtin function.

Note: This solution is actually in Javascript. I didn't noticed tags. Sorry

const temp = {
  a: [1, 2, 3, [4, 5, 6], [7], [8, 9], 10, 11]
};

let items = [];

temp.a.forEach((item) => {
  items = [
    ...items,
    ...(item.constructor.name === "Array" ? item : [item])
  ];
});
console.log(items);

Upvotes: -1

Barbaros Özhan
Barbaros Özhan

Reputation: 65373

You can apply consecutive shift transformations starting by seperating the elements to two arrays, one of which has sub-arrays, and the other has integers, namely x and y arrays . Then combine them while removing the null values, and convert stringified elements back to integers such as

[
  {
    "operation": "shift",
    "spec": {
      "a": {
        "*": {
          "@": "x.[@1]",
          "*": "y.[]"
        }
      }
    }
 },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "$1": "@1"
        }
      }
    }
 },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "$": "@(0)"
      }
    }
  },
  {
    "operation": "shift",
    "spec": {
      "*": {
        "*": {
          "@": "a"
        }
      }
    }
  },
  {
    "operation": "modify-overwrite-beta",
    "spec": {
      "*": "=toInteger"
    }
  }
]

Upvotes: 2

Related Questions