GettingStarted With123
GettingStarted With123

Reputation: 427

Mule 4.4 convert Binary payload into JSON

So I have gone through a number of questions related to this topic and tried the various options but none of them seem to work . Environment - Mule runtime 4.4 , Anypoint Studio 7.11.1 Our limitation is we are not using Enterprise edition only community edition So cannot use Transform component . However can use dataweave in setPayload / set Variable

I am receiving payload as Binary : see below : enter image description here

Data appears as JSON but is Binary

I tried changing to JSON in 'setPayload' :

%dw 2.0
import * from dw::core::Binaries     
output application/json
---
fromBase64(payload)

However I get this exception :

""Unexpected character 'ÿ' at payload@[1:30] (line:column), expected
Expecting end of input but got `ÿ, while reading `payload` as Json.  
 1| {"Employee":{"EName":"abcd"}}
                                 ^" evaluating expression: "%dw 2.0    import * from dw::core::Binaries    output application/json   
 ---    fromBase64(payload)"."

Note - also tried to check datatype of payload using :

typeOf(payload)

This too gave the same exception as above ...

Please help

Update 1: Based on comments from @aled few additional details :

I am making a GET request to my api endpoint which is reading data from an ERP system which returns data in XML Then I use a custom component that is being used to transform the XML data to JSON The o/p of this custom component is what is generating binary data which I am unable to parse enter image description here

I agree regarding Base64 that was useless I later even tried with Transform component and all of the below attempts failed with the same exception :

Attempt#1:

%dw 2.0
output application/json
---
if (isEmpty(payload.^raw)) {
"payload": "No payload!!"
}
else {
"payload": payload
}

Attempt#2:

%dw 2.0
output application/json
---
payload

The above XML payload is a short excerpt received from ERP

I need to parse the payload and identify if there is an error present in the payload .

NOTE : if I simply log the payload it all shows up fine in the logs.

is it not possible to convert binary data to json ? do you think there is something wrong in the data received either from ERP or in the XML to JSON Transformer ?

Update#2 So based on earlier comments from @aled ( thanks ! ) started focusing on payload o/p of the ERP component ( which is XML ) BEFORE it is fed to our custom transformer ( which is generating binary data ) Attaching screen print for clarity and will also paste non proprietary code : flow details

Now when I inspect the payload I can see it as :

    <?xml version = "1.0" encoding="utf-8" ?>
<Output>
   <ErrorDetails>
      <Status>1</Status>
      <Details>Invalid user or organisation</Details>
   </ErrorDetails>
   <Employee>
        <EName>abcd</EName>
    </Employee>
</Output>

and in debug mode I can see that payload is String and NOT Binary

Now I can access individual elements of the payload . Example:

<set-variable value="#[output application/java --- payload[0].ErrorDetails['Status']]" 
doc:name="extract status"  variableName="status"/>

So this gives me the value 1 which is expected and good .

Conclusions:

1 The component that interacts with ERP is generating Non Binary / stream data

2 This data as expected is parseable - all good here

3 AFTER this data is fed to our custom component - the o/p payload is now binary and am running into issues parsing it ...

Question: I know this being a custom component i cannot share code details here and thereby cannot expect inputs BUT regarding @aled comments about hexdump do you think I should try and use it against the output of the custom component ?

Once again a big thanks for your patience and help

Upvotes: 0

Views: 4523

Answers (1)

aled
aled

Reputation: 25709

Using function fromBase64() is just wrong. The value is not a Base64 string. It doesn't makes sense to try it. Using toBase64() would be useless because you would not be able to do anything with it, other than sending it somewhere else.

Your payload seems to be a JSON, and the media type is application/json in your screenshot, but it contains an UNICODE character at the end at least. You need. What are you trying to do with that payload exactly?

If the payload is truly a binary there is nothing you can do about that in the application. If you are receiving it from somewhere else check with the source. You didn't provide details of the source. Also ensure it is not your application corrupting the payload somehow.

Upvotes: 0

Related Questions