Support Questions
Find answers, ask questions, and share your expertise

Remove empty element from JSON array in NiFi

Contributor

I am creating a NiFi pipeline to send phone numbers to a phone number normalizer.  I have used EvaluateJSONPath to put all the phone numbers into NiFi attributes, then used ReplaceText to overwrite the flowfile content with the input JSON and the values of the attributes.  Here's the input JSON used in ReplaceText:

 

{
    "input":
     [
      {
        "nbr": "${TelephoneNumber1}",
        "recordId": "TelephoneNumber1"
      },
      {
        "nbr": "${TelephoneNumber2}",
        "recordId": "TelephoneNumber2"
      },
      {
        "nbr": "${TelephoneNumber3}",
        "recordId": "TelephoneNumber3"
      },
      {
        "nbr": "${TelephoneNumber4}",
        "recordId": "TelephoneNumber4"
      }
     ],
    "app_id": "test"
   }

 

 

I am trying to remove the whole entry from the "input" array if the "nbr" field is blank.  Meaning that this

 

{
    "input":
     [
      {
        "nbr": "",
        "recordId": "TelephoneNumber1"
      },
      {
        "nbr": "5551212",
        "recordId": "TelephoneNumber2"
      },
      {
        "nbr": "5558787",
        "recordId": "TelephoneNumber3"
      },
      {
        "nbr": "",
        "recordId": "TelephoneNumber4"
      }
     ],
    "app_id": "test"
}

 

Would come out looking like this:

 

{
    "input":
     [
      {
        "nbr": "5551212",
        "recordId": "TelephoneNumber2"
      },
      {
        "nbr": "5558787",
        "recordId": "TelephoneNumber3"
      }
     ],
    "app_id": "test"
}

 

 

I am currently able to accomplish this using regex and 2 ReplaceText processors to make the replacements and clean up dangling/missing commas and other formatting issues, but it basically just treats the JSON like a string instead of a data structure.

 

Is there a better way to do this that treats JSON like a data structure and doesn't risk creating invalid JSON?

0 REPLIES 0