How to achieve this expected output using jolt transforms in Apache NiFi?

27 Views Asked by At

I am trying to achieve this expected output using jolt transform, can anyone pls help me out.

input:

{
  "users": [
    {
      "username": "user1",
      "password": "password1",
      "privacy_password": "privacy1",
      "engineId": "8000000001020308"
    },
    {
      "username": "user2",
      "password": "password2",
      "privacy_password": "privacy2",
      "engineId": "8000000001020308"
    },
    {
      "username": "user3",
      "password": "password3",
      "privacy_password": "privacy3",
      "engineId": "8000000001020305"
    }
  ],
  "communities": [
    "community1",
    "community2"
  ]
}

Note:

if username is equals to user1 then expected output(then the user subjson should be removed):

{
  "users": [
    {
      "username": "user2",
      "password": "password2",
      "privacy_password": "privacy2",
      "engineId": "8000000001020308"
    },
    {
      "username": "user3",
      "password": "password3",
      "privacy_password": "privacy3",
      "engineId": "8000000001020305"
    }
  ],
  "communities": [
    "community1",
    "community2"
  ]
}

Note2: same as if it matches username is equals to user2 then user2 subjsons should be removed from the input. username will come dynamic out of three subjsons in the above input, if it matches deleted and print remaining as it.

1

There are 1 best solutions below

0
Barbaros Özhan On

Assuming a value is presented as an extra attribute "un" : "user1" added by a default transformation spec for comparison as you've suggested within the comment, then you can consider using the following complete transformation :

[
  {
    "operation": "default",
    "spec": {
      "un": "user1"
    }
  },
  { // partition the objects by "username" vs. "un"
    "operation": "shift",
    "spec": {
      "users": {
        "*": {
          "@": "&2.@1,username.@3,un.&1"
        }
      },
      "communities": "&"
    }
  },
  {
    "operation": "shift",
    "spec": {
      "users": {
        "*": {
          "&": { // matching ones
            "*": {
              "": ""
            }
          },
          "*": { // non-matching ones
            "*": {
              "@": "&4[]" // &4 stays for grabbing the literal "users" 
            }
          }
        }
      },
      "*": "&" // other elements
    }
  }
]