The contract with the client is to send JSON payload in rest web service call. In my Mule flow, I process XML and before doing web service call, convert it into JSON using
<json:xml-to-json-transformer>
which was working fine barring couple of issues. One of that is in XSD an element is defined with maxOccurs="5", but most of the time this element occurs in XML only 1 time. So the problem is:
- When an array has more than 1 entry, it is serialized correctly
- When the array has only 1 entry, it is serialized not as an array, but as a single dictionary.
So, for this XML:
<team>
<employee>
<name>Joe</name>
<surname>Bloggs</surname>
</employee>
<employee>
<name>Jane</name>
<surname>Doe</surname>
</employee>
</team>
JSON is:
{
team:{
employee:[
{
name:'Joe',
surname:'Bloggs'
},
{
name:'Jane',
surname:'Doe'
}
]
}
}
but for this XML
<team>
<employee>
<name>Joe</name>
<surname>Bloggs</surname>
</employee>
</team>
the JSON produced is:
{
team:{
employee:{
name:'Joe',
surname:'Bloggs'
}
}
}
The problem here is that client expects the value of employee to be an array (which it isn’t), violating the contract.
I read with the custom-transformer and Object Mapper, may be there is a way to specify which single element in XML should be forced to array in JSON or inject XSD. Is there a way?
