如何在 mule flow 中显示来自 json 的特定字段
How to show specific fields from json in mule flow
你好,我正在尝试创建一个简单的骡子流,我从休息 url
获取 json 数据
例如。
{
"token" : 123,
"id" : 456,
"email" : "abc@abc.com",
"status" : "Success"
}
现在我希望我的回复只显示 2 个字段,所以我的最终输出 json 会是这样的..
例如。
{
"id" : 456,
"email" : "abc@abc.com"
}
我知道这很基础。如果有人能帮助我,我会很高兴,因为我对 mule.Thanks 非常基础!
<?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>
在 http:request 组件
之后的转换消息组件中试试这个
%dw 1.0
%output application/json
---
{
id:payload.id,
email:payload.email
}
您需要在 HTTP 侦听器之后转换有效负载以删除这些字段。
您可以采用两种不同的方法:指定哪些元素应该在您的负载中,或者指定哪些元素不应该在您的负载中你的有效负载。
Anirban 的回答指定了负载中应该包含哪些元素。以下是指定哪些元素不应包含在有效载荷中的方法:
%dw 1.0
%output application/json
---
payload - "token" - "status"
由于您是 mule 的新手,您可以使用简单的设置有效负载和一些 json 路径表达式来尝试这个简单的解决方案。
<flow name="jsonTransform">
<http:listener config-ref="HTTP_Listener_Configuration" path="/jsonTransform" doc:name="HTTP" allowedMethods="POST"/>
<set-payload value="{ "id" : "#[json:id]", "email" : "#[json:email]" }" doc:name="Set Payload"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>
你好,我正在尝试创建一个简单的骡子流,我从休息 url
获取 json 数据
例如。
{
"token" : 123,
"id" : 456,
"email" : "abc@abc.com",
"status" : "Success"
}
现在我希望我的回复只显示 2 个字段,所以我的最终输出 json 会是这样的..
例如。 { "id" : 456, "email" : "abc@abc.com" }
我知道这很基础。如果有人能帮助我,我会很高兴,因为我对 mule.Thanks 非常基础!
<?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>
在 http:request 组件
之后的转换消息组件中试试这个%dw 1.0
%output application/json
---
{
id:payload.id,
email:payload.email
}
您需要在 HTTP 侦听器之后转换有效负载以删除这些字段。
您可以采用两种不同的方法:指定哪些元素应该在您的负载中,或者指定哪些元素不应该在您的负载中你的有效负载。
Anirban 的回答指定了负载中应该包含哪些元素。以下是指定哪些元素不应包含在有效载荷中的方法:
%dw 1.0
%output application/json
---
payload - "token" - "status"
由于您是 mule 的新手,您可以使用简单的设置有效负载和一些 json 路径表达式来尝试这个简单的解决方案。
<flow name="jsonTransform">
<http:listener config-ref="HTTP_Listener_Configuration" path="/jsonTransform" doc:name="HTTP" allowedMethods="POST"/>
<set-payload value="{ "id" : "#[json:id]", "email" : "#[json:email]" }" doc:name="Set Payload"/>
<json:object-to-json-transformer doc:name="Object to JSON"/>
</flow>