Lus T. Lus T. - 1 year ago 135
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" />


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

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

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


Answer Source

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" />
<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' }

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download