Search code examples
muledataweavemulesoftmule4

how to make an exception for dataweave 2.0 functions skipNullOn and isEmpty()?


I am trying to figure out how to resolve next issue: I have an array of objects which are pretty similar, but say one of the objects can have additional field. As a result of map function I need to get an array of objects which will contain not null and not empty fields only. For this purpose I am using skipNullOn=everywhere and isEmpty(). And they are working perfectly fine. But I get a new demand for one specific field, let's call it "X" we should have an exception: if this field is null or empty we should return 'false' otherwise 'true'. I can't just remove skipNullOn and isEmpty(), because in this case field "X" will appear in every object where it shouldn't be. Is it possible somehow do an exception for functions skipNullOn and isEmpty()? Please feel free to ask additional questions if I miss something. Any help appreciated.

my input:

{
  "data": {
          "array": [
            {
              "A": "A",
              "B": 1,
              "C": "2"
            },
            {
              "A": "A",
              "B": 1,
              "X": "x",
              "C": "2"
            },
            {
              "A": "A",
              "B": 1,
              "C": "2"
            }
          ]
      }
}

my current dataweave code:

%dw 2.0
output application/json skipNullOn = "everywhere"

---
payload.data."array" map (value, index) -> {
    (fieldA: {
        values: [
            value: value.A
        ]
    }) if(!isEmpty(value.A)),
    (fieldB: {
        values: [
            value: value.B
        ]
    }) if(!isEmpty(value.B)),
    (fieldC: {
        values: [
            value: value.C
        ]
    }) if(!isEmpty(value.C)),
    (fieldX: {
        values: [
            value: upper(value.X) == "X"
        ]
    }) if(!isEmpty(value.X))            
}

the desired output in case where value of "X" == null or "X" == ""

[
  {
    "fieldA": {
      "values": [
        {
          "value": "A"
        }
      ]
    },
    "fieldB": {
      "values": [
        {
          "value": 1
        }
      ]
    },
    "fieldC": {
      "values": [
        {
          "value": "2"
        }
      ]
    }
  },
  {
    "fieldA": {
      "values": [
        {
          "value": "A"
        }
      ]
    },
    "fieldB": {
      "values": [
        {
          "value": 1
        }
      ]
    },
    "fieldC": {
      "values": [
        {
          "value": "2"
        }
      ]
    },
    "fieldX": {
      "values": [
        {
          "value": false
        }
      ]
    }
  },
  {
    "fieldA": {
      "values": [
        {
          "value": "A"
        }
      ]
    },
    "fieldB": {
      "values": [
        {
          "value": 1
        }
      ]
    },
    "fieldC": {
      "values": [
        {
          "value": "2"
        }
      ]
    }
  }
]

Solution

  • Thank you both @aled and @jarus, your answers were insightful and helped greatly. I found that both solutions not fully satisfied me, maybe it is my fault and I have not explained clearly enough. They gave me insights to create a solution that met my demands. The key point was to replace null or empty values in case they are present in the specific field (in this case "X") with boolean 'false' or 'true' if value == "X". I am not claiming that my solution is better or optimal but it resolves my issue. Once again, really appreciate your help!
    Please find below a code:

    %dw 2.0
    output application/json 
    import * from dw::util::Values
    fun checkKey(key) = isEmpty(key) or upper(key) != "X"
    fun updateArrValues (array) = array map (value) -> (if (checkKey(value.X)) value  update {
        case .X  -> false
    } else if (!checkKey(value.X)) value update {
        case .X -> true
    } else value)
    ---
    updateArrValues(payload.data.array)