TechFind TechFind - 5 months ago 80
Groovy Question

mule getting data from json payload

My payload is

<json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object"/>
<logger message="#[payload]" level="INFO" doc:name="Logger"/>`

which prints ( this is the json array input given)

[{"Name":"My name","Address":"add1","Location":"NY"}]

Now, I need to get data from the payload:

<logger message="#[payload.Name]" level="INFO" doc:name="Logger"/>

it prints null. How do I get data from the payload using MEL? Is there any simpler way of getting in Groovy or xpath?


Passed single json data as:

{"Name":"My name","Address":"add1","Location":"NY"}


<logger message="#[payload.Name]" level="INFO" doc:name="Logger"/>

it prints null value.


UPDATE: Recent versions of Mule have JSON Path support in MEL, so a simple expression like #[json:Name] now works.

From the MEL tips page

MEL has no direct support for JSON. The json-to-object-transformer can turn a JSON payload into a hierarchy of simple data structures that are easily parsed with MEL.

So for your case:

<json:json-to-object-transformer returnClass="java.lang.Object" />
<logger message="#[message.payload[0].Name]" level="INFO" />

I've added [0] because what you've shown here:

[{"Name":"My name","Address":"add1","Location":"NY"}]

is not a JSON object but a JSON array containing a single JSON object.