cancel
Showing results for 
Search instead for 
Did you mean: 

How to convert JSON to XML using xslt?

Highlighted
New Contributor

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.

Tags (3)
2 REPLIES 2
Highlighted
Community Hero

Re: How to convert JSON to XML using xslt?

Hi @GeetaG 

 

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

 

if this helped answer the post, could you please mark it as 'solved'? Also if you consider whether the title of your post is relevant? Perhaps if the post is solved, it might make sense to update the Subject header field of the post to something more descriptive? This will help people when searching for problems. Ta
Highlighted
New Contributor

Re: How to convert JSON to XML using xslt?

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.

New Here?
Join us and watch the welcome video:
Top Kudoed Authors