Lus T. Lus T. - 2 months ago 30
JSON Question

Parsing JSON in Mule?

I'm trying to read a value inside a JSON string.

The payload from getValue is as simple as :
{ 'result': 'new_value' }

When I try to log the result it prints null

My code is:

<enricher target="#[flowVars.new_value]" source="#[json:result]">

<flow-ref name="getValue" />

</enricher>

<logger doc:name="Logger" level="INFO"
message="new value #[flowVars.new_value]" />

<flow name="getValue">

<http:request config-ref="http-request-tts" path="get_value" method="POST">
</http:request>

<!-- Here the payload is of type org.glassfish.grizzly.utils.BufferInputStream -->

<object-to-string-transformer/>
<json:object-to-json-transformer />

</flow>

Answer

Remove <object-to-string-transformer/> after http:request and
You need to do the following in enricher:-

<enricher target="#[flowVars.new_value]" source="#[message.payload.result]" doc:name="Message Enricher">
        <processor-chain doc:name="Processor Chain">
            <flow-ref name="getValue" doc:name="Flow Reference"/>
             <json:json-to-object-transformer  returnClass="java.lang.Object" doc:name="JSON to Object" />
       </processor-chain>
 </enricher>
<logger doc:name="Logger" level="INFO"  message="authorization_token #[flowVars.new_value]" />  

But remember, your Json should be in the format :- { "result": "new_value"} and not { 'result': 'new_value' }

Comments