m子4.4将二进制有效载荷转换为JSON
因此,我遇到了与此主题有关的许多问题,并尝试了各种选项,但它们似乎都没有用。 环境-Mule Runtime 4.4,Anypoint Studio 7.11.1 我们的局限性是我们不使用企业版Community Edition 因此无法使用变换组件。但是,可以在setPayload / set变量中使用DataWeave,
数据显示为JSON,但是二进制文件
,我尝试在'setPayload'中更改为JSON:
%dw 2.0
import * from dw::core::Binaries
output application/json
---
fromBase64(payload)
但是我得到此例外:
""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)"."
注意 - 还尝试使用有效载荷的数据类型,使用:
typeOf(payload)
这也给出了与上述相同的例外...
请帮助
更新1: 根据@Aled的评论,几个其他详细信息:
我正在向我的API端点提出请求,该请求是从ERP系统中读取数据,该数据返回XML中的数据 然后,我使用一种用于将XML数据转换为JSON的自定义组件 此自定义组件的O/P是生成我无法解析的二进制数据
我同意base64,这是没有用的 后来,我什至尝试使用变换组件,以下所有尝试失败了,但同样的例外:
尝试#1:
%dw 2.0
output application/json
---
if (isEmpty(payload.^raw)) {
"payload": "No payload!!"
}
else {
"payload": payload
}
尝试#2:
%dw 2.0
output application/json
---
payload
上述XML有效载荷是一个简短的摘录。从ERP
我需要解析有效载荷并确定有效载荷中是否存在错误。
注意:如果我简单地记录有效载荷,则在日志中都显示出正常。
是否不可能将二进制数据转换为JSON?您认为从ERP或XML到JSON Transformer收到的数据中是否有问题?
更新#2 因此,根据@Aled(谢谢!)的早期评论,开始着重于ERP组件的有效载荷O/P(XML) 之前,它被送入我们的自定义变压器(正在生成二进制数据) 附加丝网印刷以确保清晰,还将粘贴非专用代码:
现在,当我检查有效载荷时,我可以将其视为:
<?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>
在调试模式下,我可以看到有效载荷是 string and 不是二进制
有效载荷的个别要素。 示例:
<set-variable value="#[output application/java --- payload[0].ErrorDetails['Status']]"
doc:name="extract status" variableName="status"/>
因此,这给了我预期和良好的值1。
结论:
1与ERP相互作用的组件正在生成非二进制/流数据
2此数据按预期可简化 -
在此数据馈送到我们的自定义组件之后,一切都很好。现在是二进制的,并且正在遇到解决问题的问题...
问题: 我知道这是一个自定义组件,我无法在此处共享代码详细信息,从而无法期望输入,但是关于@Aled关于HEXDUMP的评论您认为我应该尝试使用它来与自定义组件的输出相比?
再次感谢您的耐心和帮助
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 :
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
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 :
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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用frombase64()的函数是错误的。该值不是base64字符串。尝试它是没有意义的。使用tobase64()是没有用的,因为除了将其发送到其他地方,您将无法使用它。
您的有效负载似乎是JSON,并且媒体类型是屏幕截图中的
application> application/json
,但至少在最后包含一个Unicode字符。你需要。您想准确地处理该有效载荷?如果有效载荷确实是二进制文件,那么您在应用程序中无能为力。如果您是从其他地方接收到的,请检查源。您没有提供来源的详细信息。另外,请确保您的应用程序不会以某种方式破坏有效载荷。
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.