Search code examples
muledataweavemulesoft

remove object from complex object if specific value is null


can someone help me please with the next problem: I need to remove from payload objects based on the next condition: if some specific key is null, I need to remove the whole object, which contains this key and value from payload. example:

{
   "issues":[
      {
         "key":"13996",
         "fields":{
            "customfield_13100":null,
            "customfield_15400":null,
            "customfield_13102":null,
            "customfield_13104":null,
            "customfield_12809":"",
            "lastViewed":null,
            "customfield_12000":[
               "870CA25F"
            ]
         }
      },
      {
         "key":"13996",
         "fields":{
            "customfield_13100":null,
            "customfield_15400":null,
            "customfield_13102":null,
            "customfield_13104":null,
            "customfield_12809":"",
            "lastViewed":null,
            "customfield_12000":null
         }
      },
      {
         "key":"13996",
         "fields":{
            "customfield_13100":null,
            "customfield_15400":null,
            "customfield_13102":null,
            "customfield_13104":null,
            "customfield_12809":"",
            "lastViewed":null,
            "customfield_12000":[
               "870CA25F"
            ]
         }
      }
   ]
}

I was trying filterObject in various variations but no luck

let's say condition is: if "customfield_12000" == null, the whole object must be removed. expected output example: `

{
   "issues":[
      {
         "key":"13996",
         "fields":{
            "customfield_13100":null,
            "customfield_15400":null,
            "customfield_13102":null,
            "customfield_13104":null,
            "customfield_12809":"",
            "lastViewed":null,
            "customfield_12000":[
               "870CA25F"
            ]
         }
      },
      {
         "key":"13996",
         "fields":{
            "customfield_13100":null,
            "customfield_15400":null,
            "customfield_13102":null,
            "customfield_13104":null,
            "customfield_12809":"",
            "lastViewed":null,
            "customfield_12000":[
               "870CA25F"
            ]
         }
      }
   ]
}

Solution

  • If I understand correctly you want to remove the item from the issues list if one specific key is null. Since in the example provided that key is in the fields object I am assuming the conditions are all similar.

    filterObject() is not appropriate because you are filtering items from an array, not key-values inside the object. filter() is the right function to filter items in an array.

    I used the update operator and created a function to encapsulate the filter() call. The dynamic selector lets us parametrize the keys used:

    %dw 2.0
    output application/json
    
    fun removeIfNullField(a, fieldname, keyname)= 
            a filter ((item) -> !(item[fieldname][keyname] is Null ))
    
    ---
    payload update {
        case issues at .issues -> removeIfNullField(issues, "fields","customfield_12000")
    }