API conversion from XML to Json

Would be great if there was a simpler way to convert api XML responses to json.
A lot of people even though it is outdated are still using XML.
We know you cleaver guys a Wappler would be able to nut this one out and make it happen.

Did you try the solution I posted?

PHP SC formatter:

<?php

namespace lib\core;

function formatter_xml2json($val) {
    $xml = simplexml_load_string(strval($val));
    return json_decode(json_encode($xml));
}

It’s pretty simple.

{{ api1.data.xml2json() }}

Yeah it did work, cheers for that. Biggest issue I’m having is with <@attributes> just can not get them to show. In the form assuming it’s something to do with the @

Can you give me a working example? I want to take a look. I suspect the person who designed that xml should probably be behind bars.

You shouldn’t start the name of an xml element with “@”

https://www.w3.org/TR/xml/#NT-NameStartChar

So if the XML is not valid there is nothing that Wappler can or should do to support that. However you would still be left with the custom formatter option to extract the value of that attribute.

But first we need to call the XML police. :smiley:

This is an example of what I am working with.

“data”: “<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n\n 1\n 1\n <product item_code=“CZ51522MTAC”>\n CZ 515 22WMR Tactical 10rnd TB\n <extended_description><p>The CZ 515 is an Australian exclusive bolt action lever release rimfire rifle designed for hunters and target shooters.</p>\r\n<p>The rifle is fitted with an automatic bolt stop in the rear position and an ergonomically designed lever for releasing the bolt. The lever release is ergonomically designed and can be transferred to the left hand side of the firearm to accommodate left handed shooters<br /><br /> This version is a modern Tactical model with pistol grip, adjustable stock (length and cheek piece), picatinny rail, full length barrel shroud and supplied with a 10 round polymer magazine.<br /> Available in this 22LR as well as <a href=“https://www.winchesteraustralia.com.au/products/CZ51522MTAC”>22WMR</a>.<br /> <br /> The CZ 515 is also available in a stylish Hunter model with an American style beechwood stock and supplied with a 10 round polymer magazine.<br />Available in&nbsp;<a href=“https://www.winchesteraustralia.com.au/products/CZ51522AMERICAN”>22LR</a> and <a href=“https://www.winchesteraustralia.com.au/products/CZ51522MAMERICAN”>22WMR</a>.</p></extended_description>\n \n <image filename=“CZ51522MTAC.tag.1.png”>\n <content_type>image/png</content_type>\n <updated_at>10:29 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524568/attachments/Product/13668/CZ51522MTAC.tag.1.png\n \n <image filename=“CZ51522MTAC.tag.2.png”>\n <content_type>image/png</content_type>\n <updated_at>10:29 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524576/attachments/Product/13668/CZ51522MTAC.tag.2.png\n \n <image filename=“CZ51522MTAC.tag.0.png”>\n <content_type>image/png</content_type>\n <updated_at>10:29 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524587/attachments/Product/13668/CZ51522MTAC.tag.0.png\n \n <image filename=“CZ51522MTAC.tag.6.png”>\n <content_type>image/png</content_type>\n <updated_at>10:30 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524606/attachments/Product/13668/CZ51522MTAC.tag.6.png\n \n <image filename=“CZ51522MTAC.tag.4.png”>\n <content_type>image/png</content_type>\n <updated_at>10:32 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524751/attachments/Product/13668/CZ51522MTAC.tag.4.png\n \n <image filename=“CZ51522MTAC.tag.3.png”>\n <content_type>image/png</content_type>\n <updated_at>10:32 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524752/attachments/Product/13668/CZ51522MTAC.tag.3.png\n \n <image filename=“CZ51522MTAC.tag.5.png”>\n <content_type>image/png</content_type>\n <updated_at>10:32 30-Mar-2020</updated_at>\n https://www.winchesteraustralia.com.au/ts1585524758/attachments/Product/13668/CZ51522MTAC.tag.5.png\n \n \n <stock_level>-219.0</stock_level>\n \n\n”

That’s not a proper XML.
It seems at first sight some kind of html scraper that tries to build an xml file with the contents of an html page. But it’s mixing html and xml markup thus not being a valid xml.

This is a direct link which does look a little different
https://www.winchesteraustralia.com.au/dealer/api/v2/products?item_code=CZ51522MTAC&token=a6e6f781-6766-4939-ab87-aaa562c56700

Not sure why the @ shows as it does not on this one.

I could also do with a way to remove the href links out of the text, if that is possible.

That XML response is a mess. I would get in touch with them and let them know.

They need to remove all html tags. They shouldn’t be there.

Specifically from the extended_description and other attributes they may have with html elements included.

<extended_description>
<p>The CZ 515 is an Australian exclusive bolt action lever release rimfire rifle designed for hunters and target shooters.</p> <p>The rifle is fitted with an automatic bolt stop in the rear position and an ergonomically designed lever for releasing the bolt. The lever release is ergonomically designed and can be transferred to the left hand side of the firearm to accommodate left handed shooters<br /><br /> This version is a modern Tactical model with pistol grip, adjustable stock (length and cheek piece), picatinny rail, full length barrel shroud and supplied with a 10 round polymer magazine.<br /> Available in this 22LR as well as <a href="https://www.winchesteraustralia.com.au/products/CZ51522MTAC">22WMR</a>.<br /> <br /> The CZ 515 is also available in a stylish Hunter model with an American style beechwood stock and supplied with a 10 round polymer magazine.<br />Available in&nbsp;<a href="https://www.winchesteraustralia.com.au/products/CZ51522AMERICAN">22LR</a> and <a href="https://www.winchesteraustralia.com.au/products/CZ51522MAMERICAN">22WMR</a>.</p>
</extended_description>

I am afraid that if I create a custom formatter to parse for you this specific response it will not work with the next one and we will never finish.

Could anyone help with a custom formatter to do this with NodeJS please?

I would appreciate any thoughts on this…

Have you tried this package?

Thanks @sid
If I install this package, call the xml via a server connect API action, set value to the returned xml, how do I then format as json?

You need to create a formatter and make use of the library to transform it. I guess it’s dicumented.

Thanks @JonL
Thats where I am struggling. I know where to place the custom formatter and have had a look at the php samples.

What did you try so far?

I looked at this https://www.npmjs.com/package/xml2js
I didn’t get very far digesting your php example at the top of the thread, I had to move on to getting other parts of the system working.

I think Jonas was refering to Wappler docs for custom formatters.
Maybe take a look at formatter/modules that users have shared on the community to get an idea on how to create one for this.

1 Like

Thanks @sid
I haven’t seen any custom formatters for NodeJS, but will try and search again and give it another go

The docs have one example:

There was another one. Can’t seem to find it now.

1 Like