Skip to content
Sashiraj Chandrasekaran
TwitterLinkedInStackOverflowGitHubEmail

XML to JSON Converter

Coding1 min read

XML to JSON converter

I recently came across this problem while designing my backend NodeJS application using Loopback framework. One of the backend systems we are connecting to can only work with SOAP APIs and we were forced to use XML. It had a key-value pair inside one of the XML fields. But the Loopback application works only with JSON and I had no idea how the JSON request would have to be to get a key-value pair on XML. I tried a couple of XML to JSON converters as I had the expected XML request with me and needed help figuring out the JSON input. Most of the converters gave me JSON which didnt quite work. Ultimately this is what worked for me.

XML key-value pairs conversion

1<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
2 <soapenv:Header/>
3 <soapenv:Body>
4 <ns2:registerCustomerRequest xmlns:ns2="http://namespace.mgtplc.com">
5 <ns2:customer>
6 <ns2:nationality>PHL</ns2:nationality>
7 </ns2:customer>
8 <ns2:data>
9 <dataValue xmlns="http://core.namespace.mgtplc.com" key="displayName">juandelacruz</dataValue>
10 </ns2:data>
11 </ns2:registerCustomerRequest>
12 </soapenv:Body>
13</soapenv:Envelope>

REST

1{
2 "soapenv:Envelope": {
3 "-xmlns:soapenv": "http://schemas.xmlsoap.org/soap/envelope/",
4 "soapenv:Body": {
5 "ns2:registerCustomerRequest": {
6 "-xmlns:ns2": "http://namespace.mgtplc.com",
7 "ns2:customer": {
8 "ns2:nationality": "PHL"
9 },
10 "ns2:data": {
11 "dataValue": {
12 "-xmlns": "http://core.namespace.mgtplc.com",
13 "-key": "displayName",
14 "#text": "juandelacruz"
15 }
16 },
17 }
18 }
19 }
20}

Most online converters converted the key field as "@key" but what loopback needs is "-key"

© 2022 by Sashiraj Chandrasekaran. All rights reserved.
Theme by LekoArts