如何在mule流中显示json中的特定字段



嗨,我正试图创建一个简单的mule流,在这里我从rest url中获取json数据
eg。{"代币":123,"id":456,"电子邮件":"abc@abc.com","status":"成功"}

现在我希望我的响应只显示2个字段,所以我的最终输出json应该是这样的。。

例如。{"id":456,"电子邮件":"abc@abc.com"}

我知道这是非常基本的。如果有人能帮我,我会很高兴的,因为我是骡子的基础。谢谢

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" 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/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
<http:request-config name="HTTP_Request_Configuration" host="reqres.in" port="8080" doc:name="HTTP Request Configuration"/>
<flow name="testFlow">
<http:listener config-ref="HTTP_Listener_Configuration" path="/test" allowedMethods="GET" doc:name="HTTP"/>
<http:request config-ref="HTTP_Request_Configuration" path="/api/users/2" method="GET" doc:name="HTTP"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>

由于您是mule的新手,您可以使用简单的set有效载荷和一些json路径表达式来尝试这个简单的解决方案。

<flow name="jsonTransform">
<http:listener config-ref="HTTP_Listener_Configuration" path="/jsonTransform" doc:name="HTTP" allowedMethods="POST"/>
<set-payload value="{ &quot;id&quot; : &quot;#[json:id]&quot;, &quot;email&quot; : &quot;#[json:email]&quot; }" doc:name="Set Payload"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>

在http之后的转换消息组件中尝试此操作:请求组件

%dw 1.0
%output application/json
---
{
id:payload.id,
email:payload.email
}

您需要在HTTP侦听器之后转换有效负载以删除这些字段。

您可以通过两种不同的方式来处理这一问题:指定应该在有效负载中的元素,或者指定不应该

Anirban的答案指定了有效负载中应该包含哪些元素。以下是如何指定哪些元素不应该出现在有效载荷中:

%dw 1.0
%output application/json
---
payload - "token" - "status"

最新更新