This documentation is for WSO2 Enterprise Service Bus version 4.7.0 . View documentation for the latest release.
Skip to end of metadata
Go to start of metadata

The PayloadFactory Mediator transforms or replaces the contents of a message. Each argument in the mediator configuration can be a static value, or you can specify an XPath or JSON expression to get the value at runtime by evaluating the provided expression against the existing SOAP message. You can configure the format of the request or response and map it to the arguments provided.

For example, in the following configuration, the values for format parameters code and price will be assigned with values that are evaluated from arguments given in the specified order.

<payloadFactory media-type="xml">
        <m:checkpriceresponse xmlns:m="http://services.samples/xsd">
    <arg xmlns:m0="http://services.samples/xsd" expression="//m0:symbol"/>
    <arg xmlns:m0="http://services.samples/xsd" expression="//m0:last"/>



<payloadFactory media-type="xml | json">
    <format ../>
		<arg (value="string" | expression=" {xpath} | {json} ")/>* 

The media-type attribute specifies whether to format the message in XML or JSON. If no media type is specified, the message is formatted in XML. If you want to change the payload type of the outgoing message, such as to change it to JSON, add the messageType property after the </payloadFactory> tag. For example:

<property name="messageType" value="application/json" scope="axis2"/>


UI Configuration

When adding a mediator to a sequence, click Transform -> PayloadFactory.

PayloadFactory Mediator Field Descriptions

  • Payload Media-Type - Specify whether to create the message payload in JSON or XML.
  • Payload Format
    • Define inline - Define the format of the payload in the text field that appears. To add content to the payload, enter variables for each value you want to add using the format $n (starting with 1 and incrementing with each additional variable, i.e., $1, $2, etc.). You will then create arguments in the same order as the variables to specify each variable's actual value.
    • Pick from Registry - Select a format definition that was stored in the Configuration or Governance registry (see Working with the Registry).
  • Arguments - For each variable in the format definition, click Add Argument and add an argument that defines the actual value.

The arguments must be entered in the same order as the variables in the format, so that the first argument defines the value for variable $1, the second argument defines the value for variable $2, etc. An argument can specify a literal string (e.g., "John") or an XPath or JSON expression that extracts the value from the content in the incoming payload.

Suppressing the namespace

To prevent the ESB from adding the default Synapse namespace in an element in the format, use xmlns="" as shown in the following example:

<ser:getPersonByUmid xmlns:ser=">
               <umid xmlns="">sagara</umid>

Working with JSON messages

By default, JSON messages are converted to XML when they are received by the PayloadFactor mediator. However, if you enable the JSON stream formatter and builder, incoming JSON messages are left in JSON format, which improves performance. To enable them, uncomment the following lines in <PRODUCT_HOME>/repository/conf/axis2/axis2.xml:

<!--messageFormatter contentType="application/json"
<!--messageBuilder contentType="application/json"

When the JSON stream formatter and builder are enabled, if you specify a JSON expression in the PayloadFactory mediator, you must use the evaluator attribute to specify that it is JSON. You can also use the evaluator to specify that an XPath expression is XML, or if you omit the evaluator attribute, XML is assumed by default. For example:


<arg xmlns:m0=" http://sample" expression="//m0:symbol" evaluator=”xml” />


<arg xmlns:m0=" http://sample" expression="//m0:symbol" />

JSON <arg expression="$" evaluator="json" />



This section provides examples of using PayloadFactory mediator to generate XML and JSON messages.

XML Example

This example is based on Sample 17: Transforming / Replacing Message Content with PayloadFactory Mediator.

<definitions xmlns="">
    <sequence name="main">
            <!-- using payloadFactory mediator to transform the request message -->
                    <m:getQuote xmlns:m="http://services.samples">
                    <arg xmlns:m0="http://services.samples" expression="//m0:Code"/>
            <!-- using payloadFactory mediator to transform the response message -->
                    <m:CheckPriceResponse xmlns:m="http://services.samples/xsd">
                    <arg xmlns:m0="http://services.samples/xsd" expression="//m0:symbol"/>
                    <arg xmlns:m0="http://services.samples/xsd" expression="//m0:last"/>

JSON Example

This example sends a JSON message to the back end.

<payloadFactory media-type="json">
    "coordinates": null,
    "created_at": "Fri Jun 24 17:43:26 +0000 2011",
    "truncated": false,
    "favorited": false,
    "id_str": "$1",
    "entities": {
        "urls": [

        "hashtags": [
                "text": "$2",
                "indices": [
        "user_mentions": [

    "in_reply_to_user_id_str": null,
    "contributors": null,
    "text": "$3",
    "retweet_count": 0,
    "id": "##",
    "in_reply_to_status_id_str": null,
    "geo": null,
    "retweeted": false,
    "in_reply_to_user_id": null,

    "source": "&lt;a 
    "in_reply_to_screen_name": null,
    "user": {
        "id_str": "##",
        "id": "##"
    "place": null,
    "in_reply_to_status_id": null
               <arg expression="$.entities.hashtags[0].text" evaluator="json"/>
               <arg expression="//entities/hashtags/text"/>
               <arg expression="//user/id"/>
               <arg expression="//user/id_str"/>
               <arg expression="$" evaluator="json"/>
               <arg expression="$.user.id_str" evaluator="json"/>
<property name="messageType" value="application/json" scope="axis2"/>
  • No labels