This is the latest release in the 6.x.x family. For EI 7.0.0, click here.

All docs This doc
||
Skip to end of metadata
Go to start of metadata

The JSON Transform mediator is used for applying XML to JSON transformations to individual artifacts instead of applying global synapse settings. This mediator can also be used to manipulate a JSON payload with the help of a JSON schema

The JSON Transform mediator is a content aware mediator.



Syntax

<jsontransform [schema="string"]>
   <property name="string" value="string"/>*
</jsontransform>

Configuration

The general parameters available for configuring the JSON Transform mediator are as follows.

Parameter NameDescription
Schema

This parameter is used for specifying the registry location of the JSON schema file.

Apart from defining a schema, you can also add properties to control XML to JSON transformation. The parameters available for configuring a property are as follows:

Parameter NameDescription
Property Name

The name of the property that needs to be overridden in the sequence. The JSON Transform mediator supports only the parameters related to XML to JSON conversion. The list of properties that are supported can be found here.

Property Value

The value that should be overridden.

Examples

Given below is a sample schema file (Schema.json) file that you can use for running the examples given below. Add this sample schema file (i.e. Schema.json) to the following registry path: conf:/Schema.json. For instructions on adding the schema file to the Registry path, see Adding a Resource.

{
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "properties": {
    "fruit": {
      "type": "string",
      "minLength": 4,
      "maxLength": 6,
      "pattern": "^[0-9]{1,45}$"
    },
    "price": {
      "type": "number",
      "minimum": 2,
      "maximum": 20,
      "exclusiveMaximum": 20,
      "multipleOf": 2.5
    }
  },
  "required": [
    "price"
  ]
}

Use the following payload to test the examples:

<jsonObject>
    <fruit>12345</fruit>
    <price>7.5</price>
    <quantity>10</quantity>
</jsonObject>

Example 1: XML to JSON transformation (overriding synapse properties)

This example will override the XML to JSON transformation properties defined in the synapse.properies configuration with the properties given by the JSON transform mediator:

<proxy xmlns="http://ws.apache.org/ns/synapse" name="transformMediatorSimpleAutoPrimitive" startOnLoad="true" transports="https http">
    <description/>
    <target>
        <inSequence>
            <property name="messageType" scope="axis2" value="application/json"/>
            <jsontransform>
                <property name="synapse.commons.json.output.autoPrimitive" value = "false"/>
            </jsontransform>
            <respond/>
        </inSequence>
    </target>
</proxy>

Output: All the numeric values have been converted to string values since the auto primitive property is defined as false.

{
    "fruit": "12345",
    "price": "7.5",
    "quantity": "10"
}

Example 2: XML to JSON transformation (synapse properties + JSON schema)

This will perform the XML to JSON transformation using the global synapse settings and then apply the JSON schema that is added from the JSON transform mediator:

<proxy name="transformMediatorSimpleSchema" startOnLoad="true" transports="https http" xmlns="http://ws.apache.org/ns/synapse">
        <description/>
        <target>
            <inSequence>
				<property name="messageType" scope="axis2" value="application/json"/>
                <jsontransform schema="conf:/Schema.json"/>
                <respond/>
            </inSequence>
        </target>
    </proxy>

Output: The 'fruit' value has been converted to string and the 'price' value has been converted to a number according to the schema definition. Please note that 'quantity' has been converted to a number because it is the default property according to the synapse.properties file.

{
    "fruit": "12345",
    "price": 7.5,
    "quantity": 10
}

Example 3: XML to JSON transformation (overriding synapse properties + applying JSON schema)

This will first override the XML to JSON transformation properties defined in the synapse.properies configuration with the properties given by the JSON transform mediator and also apply the JSON Schema given in the mediator:

<proxy xmlns="http://ws.apache.org/ns/synapse" name="transformMediatorPropertyAndSchema" startOnLoad="true" transports="https http">
    <description/>
    <target>
        <inSequence>
            <property name="messageType" scope="axis2" value="application/json"/>
            <jsontransform schema="conf:/Schema.json">
                <property name="synapse.commons.json.output.autoPrimitive" value = "false"/>
            </jsontransform>
            <respond/>
        </inSequence>
    </target>
</proxy>

Output: The 'fruit' value has been converted to a string and the 'price' value has been converted to a number according to the schema definition. Please note that 'quantity' has been converted to a string because we have overridden the global synapse.properties file.

{
    "fruit": "12345",
    "price": 7.5,
    "quantity": "10"
}
  • No labels