user3428616
user3428616

Reputation: 65

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
eg . { "token" : 123, "id" : 456, "email" : "[email protected]", "status" : "Success" }

now i want my response to show only 2 fields so my final output json would be something like this ..

eg. { "id" : 456, "email" : "[email protected]" }

I know this is very basic. I would be glad if anyone could help me out since i am very basic to 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>

Upvotes: 1

Views: 525

Answers (3)

Subham Pal
Subham Pal

Reputation: 56

Since you are new to mule u can try this simple solution using a simple set payload and some json path expression.

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

Upvotes: 2

jerney
jerney

Reputation: 2243

You will need to transform the payload after the HTTP listener to remove those fields.

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. Here's how you specify what elements should not be in the payload:

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

Upvotes: 0

Anirban Sen Chowdhary
Anirban Sen Chowdhary

Reputation: 8311

Try this in transform message component after http:request component

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

Upvotes: 0

Related Questions