Forum Discussion

YP's avatar
YP
Occasional Contributor
4 years ago

How to convert JSON to XML using xslt?

Hi All,

How can I convert the below JSON input request to XML format using XSLT I have tried with this XSLT from here https://www.quora.com/Is-there-a-way-to-convert-JSON-to-JSONX-without-using-IBM-DataPower but I didn't get desired output and it's throwing an error:"illegal character { at offset 0" in Datapower.

My input request:

{
    "SMS": [{
            "to": "+966874939494",
            "Message": "<subject+message> "
        },
        {
            "to": "+96687499490",
            "Message": "<subject+message>"
        }
    ]
}

My Expected Output:

<?xml version="1.0" encoding="UTF-8"?>
<sendReq>
                <MsgRqHdr>
                                <RqUID>SR_123</RqUID>
                                <SCId>SF12</SCId>
                                <FuncId>36</FuncId>
                                <UsrId>S12</UsrId>
                                <Dt>2020-05-26T15:12:13</Dt>
                                <Service>SMS</Service>
                </MsgRqHdr>
                <Body>
                                <Com>
                                                <Specific>
                                                                <Type>SMS</Type>
                                                </Specific>
                                                <CValue>
                                                                <Char>
                                                                                <Name>ID</Name>
                                                                </Char>
                                                                <Value>966560329031</Value>
                                                </CValue>
                                                <CValue>
                                                                <Char>
                                                                                <Name>Event</Name>
                                                                </Char>
                                                                <Value>subject&messagetext</Value>
                                                </CValue>
                                                <CValue>
                                                                <Char>
                                                                                <Name>EventParam</Name>
                                                                </Char>
                                                                <Value>English</Value>
                                                </CValue>
                                </Com>
                </Body>

 Can someone help on this please ? I never even tried Gateway script to give a try.

2 Replies

  • richie's avatar
    richie
    Community Hero

    Hi YP 

     

    do you have to use .xslt or can you use another method?

     

    Can you just outline the purpose of converting the .json to .xml please?

     

    Reason I ask is that if you want a response to come back in .xml or .json - you can use an Accept header on the request.

     

    That is, if you add an 'Accept' header to your request with a value of 'application/xml'- this will force the endpoint to send back the response in .xml rather than .json or vice versa.

     

    OR - if you just want to convert json to xml - there's an online converter here  and it generated the following:

     

    <root>
    <SMS>
    <element>
    <Message><subject+message> </Message>
    <to>+966874939494</to>
    </element>
    <element>
    <Message><subject+message></Message>
    <to>+96687499490</to>
    </element>
    </SMS>
    </root>

     

    It should be remembered that this isn't the only way to represent this in .xml - the 'to' tags could be represented as attributes rather than how they are represented above as elements - so it depends on your requirements.

     

    I didn't understand why you added in your expected response to your post - was there a reason for that?

     

    Cheers,

     

    Rich

     

    • YP's avatar
      YP
      Occasional Contributor

      Hi Richie,

       

      I have used accept header as well but still it didn't work for me and also not only .xslt need to be used. We have requirement to convert JSON to XML using either XSLT or Gateway script or JQuery.