Search code examples
jsonapache-nifijolt

Determine whether or not an array has element, Nifi


I have the following json:

{
  "a":[1,2,3,4],
  "b":[]
}

I want to know whether or not the array has elements or not. Thus the desired output required is:

{
  "a":[1,2,3,4],
  "b":[],
  "is_element_in_a":True,
  "is_element_in_b":False    
}

What will be the jolt spec expression for this?


Solution

  • You can use modify-overwrite-beta transformation spec along with size function in order to determine the size of the list is whether zero or not in the first step, and then use a shift transformation spec to print the boolean values within a conditional logic such as

    [
      {
        "operation": "modify-overwrite-beta",
        "spec": {
          "in_a": "=size(@(1,a))",
          "in_b": "=size(@(1,b))"
        }
      },
      {
        "operation": "shift",
        "spec": {
          "*": "&",
          "in_*": {
            "0": {
              "#False": "is_element_&2"
            },
            "*": {
              "#True": "is_element_&2"
            }
          }
        }
      }
    ]