Access Keys:
Skip to content (Access Key - 0)
Cancel    
Cancel   

Contents

Using the JSON Module

JSON, short for JavaScript Object Notation, is a lightweight data interchange format. It is a text-based, human-readable format for representing simple data structures and associative arrays (called objects).

JSON Bindings

Mule support binding JSON data to objects and marshaling Java object to JSON using the Jackson Framework. Jackson uses annotations to describe how data is mapped to a Java object model. For example, lets say we have an JSON file that describes a person. When we receive that JSON data we want to convert it into a Person object. The JSON looks like this-

And we have an object Person we want to create from the JSON data. We use annotations to describe how to perform the mapping. We use the @JsonAutoDetect to say that field member names map directly to JSON field names -

The EmailAddress object that is used in the emailAddresses is just another JavaBean with the @JsonAutoDetect annotation.

At this point iBeans can figure out whether to perform a JSON transforms based on the parameters of the method being called. For example -

Now if we configure this component in a flow -

 

Here we could receive the contents of the people.json file above on a JMS queue, Mule would see that Person.class is an annotated JSON object and that we had received JSON data from the JMS queue and perform the conversion.

Using the Transformers Explicitly

Often you may want to define a transformer explicitly in Mule, this is done by importing the json namespace -

Then simply configuring the transformer like any other transformer. When converting from JSON to an object form the transformer needs to define the returnClass This is the class that the Json payload will get transformed into.

When converting an object to Json, you need to specify the expected source class to convert -

Annotating objects

Jackson uses annotations to describe how to marshal and unmarshal an object to and from JSON, this is similar in concept to JAXB. However, sometimes it may not be possible to annotate the object class you want to marshal (usually because you do not have access to its source code). Instead you can define mixins. A Mixin is an interface or abstract class (needed when doing constructor injection) that defines abstract methods with Jackson annotations. The method signatures must match the methods on the object being mashalled, at runtime the annotations will be 'mixed' with the object type. To configure Mixins, use the mixin-map element or configure them on the transformer directly.

Or on the transformer directly -