Payload Factory Mediators for JSON Message
WSO2 ESB/EI PayloadFactory mediator can be used to replace the contents of a message. As an example, if the backend is accepting a different message structure than what you are receiving, you can use PayloadFactory mediator to change the message structure. Other than that, if you want to transform XML payload into JSON or JSON to XML with different message structure you can use this mediator.
Recently, I faced an issue while transforming a message into JSON which is originally read from a CSV file(using Smooks Mediator). This message can have empty values in the content sometimes and such messages look like below:
<Users>
<User>
<FirstName>Chandana</FirstName>
<MiddleName/>
</Users>
</User>
I initially wrote a payload factory mediator like below:
<payloadFactory media-type="json">
<format>
{
"UserInfo": {
"fName": "$1",
"mName": "$2"
}
}
</format>
<args>
<arg evaluator="xml" expression="//User/FirstName"/>
<arg evaluator="xml" expression="//User/MiddleName"/>
</args>
</payloadFactory>
JSON Output from the Payloadfactory mediator :
{
"UserInfo": {
"fName": "Chandana",
"mName": "{"MiddleName":null}",
}
}
So you can see that JSON output is malformed, but this will work perfectly when you have a value for MiddleName field.
To avoid this issue, what we have to do is, when we are passing arguments to the PayloadFactory mediator, we should pass text values(ex: FirstName/text()) instead of the XML object.
<payloadFactory media-type="json">
<format>
{
"UserInfo": {
"fName": "$1",
"mName": "$2"
}
}
</format>
<args>
<arg evaluator="xml" expression="//User/FirstName/text()"/>
<arg evaluator="xml" expression="//User/MiddleName/text()"/>
</args>
</payloadFactory>
Recently, I faced an issue while transforming a message into JSON which is originally read from a CSV file(using Smooks Mediator). This message can have empty values in the content sometimes and such messages look like below:
<Users>
<User>
<FirstName>Chandana</FirstName>
<MiddleName/>
</Users>
</User>
I initially wrote a payload factory mediator like below:
<payloadFactory media-type="json">
<format>
{
"UserInfo": {
"fName": "$1",
"mName": "$2"
}
}
</format>
<args>
<arg evaluator="xml" expression="//User/FirstName"/>
<arg evaluator="xml" expression="//User/MiddleName"/>
</args>
</payloadFactory>
JSON Output from the Payloadfactory mediator :
{
"UserInfo": {
"fName": "Chandana",
"mName": "{"MiddleName":null}",
}
}
So you can see that JSON output is malformed, but this will work perfectly when you have a value for MiddleName field.
To avoid this issue, what we have to do is, when we are passing arguments to the PayloadFactory mediator, we should pass text values(ex: FirstName/text()) instead of the XML object.
<payloadFactory media-type="json">
<format>
{
"UserInfo": {
"fName": "$1",
"mName": "$2"
}
}
</format>
<args>
<arg evaluator="xml" expression="//User/FirstName/text()"/>
<arg evaluator="xml" expression="//User/MiddleName/text()"/>
</args>
</payloadFactory>
Comments