2

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.

HMT
  • 2,093
  • 1
  • 19
  • 51
  • Can you share the code? – Ryan Carter May 13 '20 at 08:35
  • I have recieved this json object from an external api – HMT May 13 '20 at 08:49
  • Please provide more information - which version of mule, sample payload, etc. You say you're checking if the JSON object is empty or null; do you mean the entire payload or a specific key? Please also provide the specifics about how you're trying to set the variable. – Michael Jones May 13 '20 at 12:42
  • Add a logger right after your API call and do `output application/json --- payload` to see what kind of data you are receive from the API. You can try other mime types if JSON does not work. – George May 13 '20 at 14:53
  • None of the suggested answers actually work in dw 2.0. The payload, of type application/json, is literally null, and even isEmpty(payload) fails with the original poster's error. – AndrWeisR Feb 17 '21 at 04:37

2 Answers2

0

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

Alex
  • 4,457
  • 2
  • 20
  • 59
  • isEmpty(vars.obj) is not returning true – HMT May 13 '20 at 12:07
  • Then it is not empty. Log it and see. In my example third line is for none existing variable. It is returning true. It is possible that vars.obj in the Linux is different because of text/binary files, vs , etc. My point is - log this varaiable and double check that it meets your expectation. – Alex May 13 '20 at 12:53
  • Also pay attention for WhiteSpaceString. I had updated my answer with image – Alex May 13 '20 at 13:05
0

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.

AndrWeisR
  • 1,110
  • 11
  • 21