简体   繁体   English

Mule DataWeave JSON内容为空

[英]Mule DataWeave JSON content is null

Mule version: 3.7.3 ule子版本:3.7.3

Anypoint Studio: 5.4.0 Anypoint Studio:5.4.0

To learn DataWeave, I've created a simple flow that executes twitter search, loops through each tweet, attempts to use DataWeave to condense the payload, then logs the results. 为了学习DataWeave,我创建了一个简单的流程,该流程执行Twitter搜索,遍历每个推文,尝试使用DataWeave压缩有效负载,然后记录结果。 The issue I'm running into is that the json seems to be null coming out of DW. 我遇到的问题是从DW出来的json似乎为空。

Tweet Text before DW: DW之前的推文:

Welcome to the world of #APILedConnectivity Lebron James! Your request has been processed by #MuleSoft #AnypointPlatform @ 1986

The payload coming out of DW is not null: 来自DW的有效载荷不为null:

org.mule.api.processor.LoggerMessageProcessor: DW Payload: com.mulesoft.weave.mule.WeaveMessageProcessor$WeaveOutputHandler@1657d772

However, the 'tweet' json element is null when outputting to logs: 但是,输出到日志时,“ tweet” json元素为null:

org.mule.api.processor.LoggerMessageProcessor: Found Tweet: null

Here's my sample mule flow: 这是我的样本m子流程:

    <?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:json="http://www.mulesoft.org/schema/mule/json" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:twitter="http://www.mulesoft.org/schema/mule/twitter" xmlns="http://www.mulesoft.org/schema/mule/core" xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
    xmlns:spring="http://www.springframework.org/schema/beans" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/twitter http://www.mulesoft.org/schema/mule/twitter/current/mule-twitter.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
http://www.mulesoft.org/schema/mule/json http://www.mulesoft.org/schema/mule/json/current/mule-json.xsd">
    <flow name="test-dataweaveFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/test" doc:name="HTTP"/>
        <twitter:search config-ref="TwitterConnection" query="#[json:hashtag]" doc:name="Twitter"/>
        <foreach collection="#[payload.tweets]" doc:name="For Each">
            <logger message="Payload before DW: #[payload.text]" level="INFO" doc:name="Logger"/>
            <dw:transform-message metadata:id="99e88418-ce6a-4d18-a900-6c35d7f36c83" doc:name="Transform Message">
                <dw:input-payload doc:sample="QueryResult.dwl"/>
                <dw:set-payload><![CDATA[%dw 1.0
%output application/json
---
{ 
    tweet: {
        screenName: payload.user.screenName,
        tweetId: payload.id,
        tweetContent: payload.text
    }
}
]]></dw:set-payload>
            </dw:transform-message>
            <logger message="DW Payload: #[payload]" level="INFO" doc:name="Logger"/>
            <logger message="Found Tweet: #[payload.tweet]" level="INFO" doc:name="Logger"/>
        </foreach>
        <json:object-to-json-transformer doc:name="Object to JSON"/>
    </flow>
</mule>

Screenshot of the mapping: 映射的屏幕截图: 在此处输入图片说明

Any ideas where I might be messing things up? 有什么想法可能会搞砸我吗?

Don't put the DataWeave transformer in the for each scope! 不要将DataWeave变压器放在每个示波器中!

You can iterate in DataWeave, that will give you a better result probably. 您可以在DataWeave中进行迭代,这可能会给您带来更好的结果。

You will create a map from the payload, the map operator accepts a collection. 您将根据有效负载创建一个地图,该地图操作员接受一个集合。

The twitter connector will give you a collection of JSON objects. twitter连接器将为您提供JSON对象的集合。

The clue here is, drag the DataWeave transformer after the Twitter connector. 提示是,将DataWeave变压器拖到Twitter连接器之后。

The input data in DataWeave will be provided and you will have autocomplete in you DataWeave script. 将提供DataWeave中的输入数据,并且您将在DataWeave脚本中具有自动完成功能。

Docs: https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation 文件: https : //docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-reference-documentation

Some examples in how to map form JSON here: 有关如何在此处映射JSON的一些示例:

https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-examples#json-to-xml https://docs.mulesoft.com/mule-user-guide/v/3.7/dataweave-examples#json-to-xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM