I'm getting an XML response and need to convert it to the JSON array.
XML response is as below:
<jsonObject>
<message>
<status>Success</status>
<timestam>2022-12-04T17:51:15.9841813+11:00</timestam>
<resultCount>35</resultCount>
<totalCount>35</totalCount>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>97282A08</text>
<value>97282A08</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>185804A09</text>
<value>185804A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>241248A09</text>
<value>241248A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>258111A09</text>
<value>258111A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>429398A11</text>
<value>429398A11</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>450962A11</text>
<value>450962A11</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>588602A12</text>
<value>588602A12</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>618329A12</text>
<value>618329A12</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>624645A12</text>
<value>624645A12</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>643029A12</text>
<value>643029A12</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>655593A12</text>
<value>655593A12</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>684292A12</text>
<value>684292A12</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>903240A14</text>
<value>903240A14</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1031807A15</text>
<value>1031807A15</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1353624A17</text>
<value>1353624A17</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1353626A17</text>
<value>1353626A17</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1436375A18</text>
<value>1436375A18</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1455356A18</text>
<value>1455356A18</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1500185A18</text>
<value>1500185A18</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1511985A18</text>
<value>1511985A18</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1625059A19</text>
<value>1625059A19</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1630914A19</text>
<value>1630914A19</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1741745A20</text>
<value>1741745A20</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>1878082A21</text>
<value>1878082A21</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>2061825A22</text>
<value>2061825A22</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>2061829A22</text>
<value>2061829A22</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>2061830A22</text>
<value>2061830A22</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>5067/1993</text>
<value>5067/1993</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>497/1998</text>
<value>497/1998</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>954/1998</text>
<value>954/1998</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>206A09</text>
<value>206A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>34A09</text>
<value>34A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>15187A09</text>
<value>15187A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>15188A09</text>
<value>15188A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>18122A04</text>
<value>18122A04</value>
</listItems>
</AlicationNumber>
</fields>
</message>
I'm using messageType "application/JSON" and I get the result below:
{
"message": {
"status": "Success",
"timestam": "2022-12-04T17:51:15.9841813+11:00",
"resultCount": 35,
"totalCount": 35,
"fields": [
{
"AlicationNumber": {
"listItems": {
"text": "97282A08",
"value": "97282A08"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "185804A09",
"value": "185804A09"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "241248A09",
"value": "241248A09"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "258111A09",
"value": "258111A09"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "429398A11",
"value": "429398A11"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "450962A11",
"value": "450962A11"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "588602A12",
"value": "588602A12"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "618329A12",
"value": "618329A12"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "624645A12",
"value": "624645A12"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "643029A12",
"value": "643029A12"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "655593A12",
"value": "655593A12"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "684292A12",
"value": "684292A12"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "903240A14",
"value": "903240A14"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1031807A15",
"value": "1031807A15"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1353624A17",
"value": "1353624A17"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1353626A17",
"value": "1353626A17"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1436375A18",
"value": "1436375A18"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1455356A18",
"value": "1455356A18"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1500185A18",
"value": "1500185A18"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1511985A18",
"value": "1511985A18"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1625059A19",
"value": "1625059A19"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1630914A19",
"value": "1630914A19"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1741745A20",
"value": "1741745A20"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "1878082A21",
"value": "1878082A21"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "2061825A22",
"value": "2061825A22"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "2061829A22",
"value": "2061829A22"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "2061830A22",
"value": "2061830A22"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "5067/1993",
"value": "5067/1993"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "497/1998",
"value": "497/1998"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "954/1998",
"value": "954/1998"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "206A09",
"value": "206A09"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "34A09",
"value": "34A09"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "15187A09",
"value": "15187A09"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "15188A09",
"value": "15188A09"
}
}
},
{
"AlicationNumber": {
"listItems": {
"text": "18122A04",
"value": "18122A04"
}
}
}
]
}
}
But the format that I need to get is as below:
{
"message": {
"status": "Success",
"timestam": "2022-12-04T17:51:15.9841813+11:00",
"resultCount": 35,
"totalCount": 35,
"fields": [
{
"AlicationNumber": {
"listItems": [
{
"text": "97282A08",
"value": "97282A08"
},
{
"text": "185804A09",
"value": "185804A09"
},
{
"text": "241248A09",
"value": "241248A09"
},
{"text": "258111A09",
"value": "258111A09"
},
...
]
}
}
]
}}
I have used JSON transformer as well but didn't change the response format. I used the below configuration for the JSON transformer
{
"$schema": "http://json-schema.org/draft-04/schema#",
"type": "object",
"properties": {
"fields": {
"type": "object",
"properties": {
"ApplicationNumber": {
"type": "object"
},
"listItems":{
"type": "Array",
"properties":{
"text":{
"type":"string"
},
"value":{
"type":"string"
}
}
}
}
}
}
}
Can you please help me with how can I achieve this? I'm using wso2 integration studio 7.2.
You can use the PayloadFactory Mediator
for this. Take a look at the example below.
<payloadFactory media-type="json">
<format>{
"message": {
"status": "$1",
"timestam": "$2",
"resultCount": $3,
"totalCount": $4,
"fields": [
{
"AlicationNumber": $5
}
]
}}
</format>
<args>
<arg evaluator="xml" expression="//message/status" />
<arg evaluator="xml" expression="//message/timestam" />
<arg evaluator="xml" expression="//message/resultCount" />
<arg evaluator="xml" expression="//message/totalCount" />
<arg evaluator="xml" expression="//sy:AlicationNumber/sy:listItems" xmlns:sy="htt://ws.aache.org/ns/synase"/>
</args>
</payloadFactory>
Complete API
<?xml version="1.0" encoding="UTF-8"?>
<api context="/jsontest" name="HelloWorld" xmlns="http://ws.apache.org/ns/synapse">
<resource methods="POST">
<inSequence>
<payloadFactory media-type="json">
<format>{
"message": {
"status": "$1",
"timestam": "$2",
"resultCount": $3,
"totalCount": $4,
"fields": [
{
"AlicationNumber": $5
}
]
}}
</format>
<args>
<arg evaluator="xml" expression="//message/status"/>
<arg evaluator="xml" expression="//message/timestam"/>
<arg evaluator="xml" expression="//message/resultCount"/>
<arg evaluator="xml" expression="//message/totalCount"/>
<arg evaluator="xml" expression="//sy:AlicationNumber/sy:listItems" xmlns:sy="htt://ws.aache.org/ns/synase"/>
</args>
</payloadFactory>
<log category="DEBUG" level="full"/>
<respond/>
</inSequence>
<outSequence/>
<faultSequence/>
</resource>
</api>
Reuest
curl --location --request POST 'http://localhost:8290/jsontest' \
--header 'Content-Type: application/xml' \
--data-raw '<jsonObject>
<message>
<status>Success</status>
<timestam>2022-12-04T17:51:15.9841813+11:00</timestam>
<resultCount>35</resultCount>
<totalCount>35</totalCount>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>97282A08</text>
<value>97282A08</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>185804A09</text>
<value>185804A09</value>
</listItems>
</AlicationNumber>
</fields>
<fields
xmlns="htt://ws.aache.org/ns/synase">
<AlicationNumber>
<listItems>
<text>241248A09</text>
<value>241248A09</value>
</listItems>
</AlicationNumber>
</fields>
</message>
</jsonObject>'
Response
{
"message": {
"status": "Success",
"timestam": "2022-12-04T17:51:15.9841813+11:00",
"resultCount": 35,
"totalCount": 35,
"fields": [
{
"AlicationNumber": {
"listItems": [
{
"text": "97282A08",
"value": "97282A08"
},
{
"text": "185804A09",
"value": "185804A09"
},
{
"text": "241248A09",
"value": "241248A09"
}
]
}
}
]
}
}