Hi,

Say for example used a proxy below [1] to convert an xml to json.

[1]

<proxy xmlns="http://ws.apache.org/ns/synapse"; name="XmlToJSON"
transports="https,http" statistics="disable" trace="disable"
startOnLoad="true">
    <target>
        <inSequence>
            <property name="messageType" value="application/json"
scope="axis2"/>
            <respond/>
        </inSequence>
    </target>
    <description/>
</proxy>

using a payload like [2] returns a response like [3] with a json array.

[2]
    <users>
    <user>user1</user>
    <user>user2</user>
    </users>

[3]

{
  "users": {
    "user": [
      "user1",
      "user2"
    ]
  }
}

using a payload like [4] returns a response like [5] without square
brackets, since there's only one element.

[4]
        <users>
                <user>user1</user>
        </users>

[5]

{
  "users": {
    "user": "user1"
  }
}

But I need to get this user1 in an array even if there's only one
element in the array. I can however use a payload factory with a
filter mediator like below to achieve this. Wondering if there's any
other simple way to achieve without this? Your input is appreciated.

[6]

<filter source="json-eval($.StockQuotes.Stock.Symbol)" regex="null">
    <then>
        <payloadFactory media-type="json">
            <format>{ "StockQuotes": { "Stock": $1 } }</format>
            <args>
                <arg evaluator="json" expression="$.StockQuotes.Stock"/>
            </args>
        </payloadFactory>
    </then>
    <else>
        <payloadFactory media-type="json">
            <format>{ "StockQuotes": { "Stock": [ $1 ] } }</format>
            <args>
                <arg evaluator="json" expression="$.StockQuotes.Stock"/>
            </args>
        </payloadFactory>
    </else>
</filter>

Shazni Nazeer
Mob : +94 777737331
LinkedIn : http://lk.linkedin.com/in/shazninazeer
Blog : http://shazninazeer.blogspot.com
_______________________________________________
Dev mailing list
[email protected]
http://wso2.org/cgi-bin/mailman/listinfo/dev

Reply via email to