简体   繁体   English

如何在m子流中显示JSON中的特定字段

[英]How to show specific fields from json in mule flow

Hi i am trying to create a simple mule flow where i get the json data from a rest url 嗨,我正在尝试创建一个简单的m流,其中我从其他网址获取json数据
eg . 例如。 { "token" : 123, "id" : 456, "email" : "abc@abc.com", "status" : "Success" } {“令牌”:123,“ id”:456,“电子邮件”:“ abc@abc.com”,“状态”:“成功”}

now i want my response to show only 2 fields so my final output json would be something like this .. 现在我希望我的响应仅显示2个字段,所以我的最终输出json将是这样的..

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

I know this is very basic. 我知道这是非常基本的。 I would be glad if anyone could help me out since i am very basic to mule.Thanks ! 我会很高兴有任何人可以帮助我,因为我对m子非常基础。谢谢!

                <?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>

Try this in transform message component after http:request component 在http:request组件之后的转换消息组件中尝试此操作

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

You will need to transform the payload after the HTTP listener to remove those fields. 您将需要在HTTP侦听器之后转换有效负载以删除这些字段。

You can approach this two different ways: Specify what elements should be in your payload, or specify what elements should not be in your payload. 您可以采用两种不同的方法:指定有效负载中包含哪些元素,或指定有效负载中包含哪些元素。

Anirban's answer specifies what elements should be in the payload. Anirban的答案指定了有效载荷中应该包含哪些元素。 Here's how you specify what elements should not be in the payload: 这是您指定有效负载中不应包含哪些元素的方法:

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

Since you are new to mule u can try this simple solution using a simple set payload and some json path expression. 由于您是m子的新手,您可以尝试使用简单的有效载荷和一些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>

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

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