Contact Free trial Login

Migrating the JSON Module

A new JSON module in Mule 4 replaces the one that was bundled in with Mule 3 for schema validation.

To migrate transformations involving JSON, MuleSoft recommends that you use DataWeave.

Migrating Transformers

In Mule 3, many components required the payload to be a POJO so that its fields could be accessed. In Mule 4, DataWeave can be used as a query language directly on the JSON payload, which completely avoids the need to convert the payload to a POJO as an intermediate step.

JSON to Object Transformer

For mapping rules that are configured for the json:json-to-object-transformer, you can express the rules in DataWeave.

  • In Mule 3:

    Example: Mixin used in the Mapper Configuration

    public abstract class AppleMixin
    {
        AppleMixin(@JsonProperty("bitten") boolean wasBitten)
        {
            super();
        }
    
    }

    Example: Transformer Referencing the Mapper

    <json:mapper name="myMapper">
        <json:mixin mixinClass="org.mule.module.json.transformers.AppleMixin"
                        targetClass="org.mule.tck.testmodels.fruit.Apple"/>
    </json:mapper>
    
    <flow name="flow">
        <json:json-to-object-transformer returnClass="org.mule.tck.testmodels.fruit.Apple"
                                         mapper-ref="myMapper"/>
    </flow>
  • In Mule 4:

    Example: Using DataWeave as the Mapper

    <flow name="flow">
        <ee:transform>
            <ee:message>
                <ee:set-payload>
                    <![CDATA[%dw 2.0
                        type apple = Object { class: "org.mule.tck.testmodels.fruit.Apple"}
                        output application/java
                        ---
                        {
                            wasBitten = bitten
                        } as apple
                    ]]>
                </ee:set-payload>
            </ee:message>
        </ee:transform>
    </flow>

You don’t have to define a mapper or a mixin class in Mule 4. The mapping rules are defined using DataWeave code.

Object to JSON Transformer

The mapping rules for the json:object-to-json-transformer are also expressed using DataWeave:

In Mule 3:

  • Example: Transformer with Mixins

    <flow name="flow">
        <json:object-to-json-transformer returnClass="org.mule.tck.testmodels.fruit.Apple">
            <json:serialization-mixin mixinClass="org.mule.module.json.transformers.AppleMixin"
                                      targetClass="org.mule.tck.testmodels.fruit.Apple"/>
    
        </json:object-to-json-transformer>
    </flow>

In Mule 4:

  • Example: Using DataWeave as the Mapper

    <flow name="flow">
        <ee:transform>
            <ee:message>
                <ee:set-payload>
                    <![CDATA[%dw 2.0
                        output application/json
                        ---
                        {
                            bitten = wasBitten
                        }
                    ]]>
                </ee:set-payload>
            </ee:message>
        </ee:transform>
    </flow>

In Mule 4 you can set the output format in the transformation. In this case, the expression defines the output as application/json, so the transformation function returns a JSON payload.

Migrating XSLT Operations

In Mule 4, the JSON module does not provide a component that transforms the JSON based on an XSLT mapping. Possible ways to handle this case are:

  • Migrate the XSLT to a DataWeave transformation (recommended)

  • Use XSLT support of the XML module, first converting the payload to XML.

Mule 4 Example: Using XSLT to Transform JSON
<flow name="flow">
    ...
	<xml-module:xslt-transform>
	    <xml-module:content>#[output application/xml --- payload]</xml-module:content>
	    <xml-module:xslt>
	        (the XSLT transformation) ...
	    </xml-module:xslt>
	</xml-module:xslt-transform>
	...
</flow>

Was this article helpful?

💙 Thanks for your feedback!

Edit on GitHub