how to check whether the JSON recieved is empty/ nulll in mule 4

Viewed 4351

I am trying to check whether the json object is empty/ null but I am getting the following error:

org.mule.runtime.core.api.expression.ExpressionRuntimeException: "Unable to parse empty input, while reading `obj` as Json.

1|
   ^
Trace:
  at main (line: 1, column: 1)" evaluating expression: "isEmpty(vars.obj)".

also, I observed that the variable looks like this in the mule debugger :

obj= 

Please suggest a solution.

2 Answers

All inputs should be checked properly as described here https://simpleflatservice.com/mule4/DoubleAndTripleCheckTheInput.html

null can be checked in advance like

vars.obj == null or isEmpty(vars.obj)

However it is not necessary. isEmpty function works perfectly fine with nulls.

%dw 2.0
output application/json
---
{
    reallyEmpty: isEmpty(''),
    nullOrReallyEmpty: isEmpty(null),
    unknownVaraiable: isEmpty(vars.xyz)
}

output

{
  "reallyEmpty": true,
  "nullOrReallyEmpty": true,
  "unknownVaraiable": true
}

Also pay attention for WhiteSpaceString

enter image description here

If the MIME type of the payload is application/json, then an empty object is not valid JSON and cannot be parsed. The payload needs to be {}. See the Mule doc. The solution there is for the client to send {} if the MIME type is application/json.

Related