xml to json conversion in wso2
Asked Answered
S

2

5

When I am trying to convert XML to Json using XSLT mediator in wso2 I am getting "Payload could not be written as JSON." error. Can anyone help to me resolve this.

Thanks in advance

Stet answered 31/10, 2015 at 13:31 Comment(2)
will be better if you can provide more infomation, ie the configurations you've donePyroxene
@Rajeev here is my configurations which i have done <xquery key="filenames.xq"></xquery><xslt key="filename.xslt" source="$body"><params/></xslt><property name="messageType" value="application/json" scope="axis2"/><call><endpoint/></call>Stet
M
7

Why you do not use payload factory? this is a best way for converting xml to Json.

for example:

<payloadFactory media-type="json">
    <format>{ "error": "0", "message": "$1", "data": $2 }</format>
        <args>
            <arg evaluator="json" expression="$.USER.description"/>
            <arg evaluator="json" expression="$.USER"/>
        </args>
</payloadFactory>
<property name="ContentType" value="application/json" scope="axis2"/>

Or use this property for send message:

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

The media-type attribute specifies whether to format the message in XML or JSON. In this example i use JSON to convert this xml:

<USER>
    <description>some Notes</description>
    <others></others>
</USER>

and the ESB result:

{
    "error" : "0",
    "message" : "Some notes",
    "data"  : {
        "description" : "Some notes",
        "others" : ""
    }
}

for more information about payload factory please see this link: https://docs.wso2.com/display/ESB490/PayloadFactory+Mediator

Update:

you must use switch case or filter mediator. for example this is a switch case sample. you must complete regex:

 <switch source="//body">
        <case regex="">
           <payloadFactory media-type="json">

           </payloadFactory>
        </case>
 </switch>

or you can use filter. In this example, the Filter match your given regular expression or XPath. If this evaluation returns true, it will send the true json. If the evaluation returns false, it will return empty json.

<filter (source="[XPath|json-eval(JSONPath)]" regex="string") | xpath="[XPath|json-eval(JSONPath)]">
    <then>
        <payloadFactory media-type="json">
            <format>{josn:"body"}</format>
            <args>your args<args/>
        </payloadFactory>
    </then>
    <else>
        <payloadFactory media-type="json">
            <format>{}</format>
            <args/>
        </payloadFactory>
    </else>
</filter>

This link should help you: https://docs.wso2.com/display/ESB490/Filter+Mediator

Malefactor answered 2/11, 2015 at 19:53 Comment(4)
I had tried this too, but there is a problem, when my <arg> don't have any value, then i should not add that to my <format>, so for that i have used xslt. it should be something like this : if arg0 != "" then json will be, {"somekey":"arg0"} else my json will be {}Stet
thanks for ur reply. it will be helpful, if i have only one <arg>, what if we have multiple <arg>? do we need to use multiple filters? instead we can use xslt.Stet
you must use switch case on JsonObject and define your rules with regex. it's possible. i try all solutions about converting and transforming message and Payload factory is the best for me. try this solution and inform me about your experience.Malefactor
Thanks for your answer. Currently am using the payload factory only. But it is not suite exactly my use case (based on the <arg> values need to <format> my json), that is the reason I am trying with xslt. By using payload factory it is not possible.Stet
M
5

If you are trying to convert a soap service to rest, you can do it by specifying message type as (in a rest api):

 <property name="messageType" value="application/json" scope="axis2"/>
Marquet answered 1/11, 2015 at 1:45 Comment(1)
i have tried with this. but it is not helpful. my configurations are something like this: <xquery key="filenames.xq"></xquery><xslt key="filename.xslt" source="$body"><params/></xslt><property name="messageType" value="application/json" scope="axis2"/><call><endpoint/></call>.Stet

© 2022 - 2024 — McMap. All rights reserved.