4
votes

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?

EDIT

Passed single json data as:

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

logger:

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

it prints null value.

2

2 Answers

12
votes

In Mule ESB 3.7.0

This work:

#[json:Name]
8
votes

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.