0

I have this xml which needed to be processed and converted to JSON:

<jsonObject>
    <number>1234</number>
    <emptyString/>
    <string>hello</string>
</jsonObject>

When it runs through JsonStreamBuilder like this:

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

I get JSON like that:

{
  "number":1234,
  "emptyString": null,
  "string":"hello"
}

So I am wondering is there any way that I can configure JsonStreamBuilder to treat empty tag not as null, but as empty string, furthermore I want numbers to be treated as strings as well.

Maybe I can modify the default XML to fit my needs?

BTW I am using WSO2 EI 6.1.1

Constantine
  • 381
  • 1
  • 3
  • 10

3 Answers3

4

The default behavior of JsonFormatter is to allow primitive types in the JSON output. This means any content that is a valid number will be represented in the JSON message as a number.

As a solution for this issue, you could disable, the conversion to primitive type after adding below property to the synapse.properties file of EI_HOME/conf directory.

synapse.commons.json.output.autoPrimitive=false 

However, this property is a global property, so it will be effected to all the services (Proxy services and the APIs). So this means any string that is a valid number will not be converted to a number.

According to your issue, you need to disable the conversion of String to Integer. You could solve this, adding below properties into EI_HOME/conf/synapse.properties file also.

synapse.commons.json.output.autoPrimitive=true

synapse.commons.json.output.disableAutoPrimitive.regex=^[1-9]*$

In this case, auto primitive disables for the numbers only.

NOTE: Once autoprimitive is enabled, some fields can be excluded from the auto primitive functionality based on the regular expression defined in the synapse.properties file.

  • And the empty tag as the not null issue, you can resolve following the above-mentioned blog post.
Dil
  • 41
  • 1
  • Great explanation, but how would OP exclude empty strings from the auto primitive functionality? Since that seems to be what he needs – ophychius Dec 31 '18 at 10:31
  • You can follow https://medium.com/@lashan/class-mediator-to-handle-null-values-in-json-payload-wso2-ei-34a43246f0f blog to address this issue – Dil Dec 31 '18 at 18:18
  • Dear Dil, the purpose of SO is so that the answer is recorded here. A link is fine as support but the solution should be here – ophychius Jan 01 '19 at 10:33
  • Thanks for the answer, but that link explains the opposite of what I need to achieve json null to xml empty tag, whereas I need to transform xml empty tag to empty string in json. And that task has higher priority. Nevertheless thanks for the answer. – Constantine Jan 17 '19 at 08:10
0

This looks like restriction in wso itlsef, documentation states that is desired behavior. To overcome issue have try script mediator WSO2ESB: Property setting not accepting empty value or getting even lower (more complicated) https://medium.com/@lashan/class-mediator-to-handle-null-values-in-json-payload-wso2-ei-34a43246f0f

simar
  • 1,782
  • 3
  • 16
  • 33
0

If you were using a more recent WSO version, there is a solution: https://docs.wso2.com/display/EI630/Working+with+Message+Payloads#WorkingwithMessagePayloads-EmptyXMLelementswiththe'nil'attribute

You can set synapse configs locally with the help of JSON Transform mediator

pihentagy
  • 5,975
  • 9
  • 39
  • 58