0

I am using the MuleSoft Transform Message component to initialize variables testIP and testDomain. It works well when the payload is not empty, but throws an exception when the payload is null or empty.

The payload is received from an HTTP Listener with content type application/json and the body of the request is empty.

How can I make testIP or testDomain to false when the payload is null?

 <ee:transform doc:name="init vars" doc:id="9f281802-c37b-4730-9932-4031337f4a23" >
                <ee:message >
                </ee:message>
                <ee:variables >
                    <ee:set-variable variableName="testIP" ><![CDATA[%dw 2.0
    output application/java
    ---
    if (payload != null and payload.ip? and !(payload.ip is String and payload.ip == "")) 
      true 
    else 
      false]]></ee:set-variable>
                    <ee:set-variable variableName="testDomain" ><![CDATA[%dw 2.0
    output application/java
    ---
    if (payload != null and payload.domain? and !(payload.domain is String and payload.domain == "")) 
      true 
    else 
      false
    ]]></ee:set-variable>
                </ee:variables>
    </ee:transform>

Error I got is

"Unable to parse empty input, while reading `payload` as Json.
 
1| 
   ^" evaluating expression: "%dw 2.0
output application/java
---
if (payload != null and payload.ip? and !(payload.ip is String and payload.ip == "")) 
  true 
else 
  false".
1
  • Which errors are you getting? edit the question to add them. Since both checks are the same a minimal reproducible example requires one of them (ip or domain). Commented Jul 11 at 15:01

1 Answer 1

1

I suspect that you could replace the entire condition with:

!isEmpty(payload.ip)

There is one case that would be different but I don't think you want your expression to return true for payload {ip:null}.

Tests:

%dw 2.0
output application/json
var payloads=[null, {}, {ip:null}, {ip:""}, {ip:"1234"}]
---
payloads map {
    testIp: if ($ != null and $.ip? and !($.ip is String and $.ip == "")) 
                true 
            else 
                false
    ,
    isEmpty: !isEmpty($.ip)
}

Output:

[
  {
    "testIp": false,
    "isEmpty": false
  },
  {
    "testIp": false,
    "isEmpty": false
  },
  {
    "testIp": true,
    "isEmpty": false
  },
  {
    "testIp": false,
    "isEmpty": false
  },
  {
    "testIp": true,
    "isEmpty": true
  }
]

Note that generally in programming if you use an if to return a boolean you can just omit the if/else and just return the boolean value from the condition.

UPDATE: since the specific problem is because the application is receiving a null body with Content-Type set as application/json, which I understand is an invalid JSON content, the expression I suggested will fail. This is because DataWeave can not parse invalid JSON content. A solution is to wrap the expression with try() from the Runtime module, so if the expression fails to evaluate we can overwrite the result to false in the orElse.

UPDATE 2: converted the expression into a function. The parameter is a closure so it is not evaluated before the check.

import * from dw::Runtime
fun checkValid(x)=try(()->!isEmpty(x())) orElse false
---
checkValid(() -> payload.ip)
Sign up to request clarification or add additional context in comments.

6 Comments

Problem is due to payload is retrieved via HttpListener, if content type is set as application/json but actually as empty, it will cause problem for transform message component
The problem is rather with that invalid JSON payload than with DataWeave. The question didn't had enough context to understand the root issue. DataWeave can not parse invalid JSON values, same as it can not parse invalid XML.
yes, you are right
I updated the solution for this situation.
Using try block to handle error /set result is really smart idea!
Created a function for reuse.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.