Hot to make a GET request and convert the xml response to json?

Hi,

I am very new to the babylon, i am making a GET request to my azure table and i could get the response back as a xml format by default, i am trying to convert the xml response to json by using the xml2js, but unfortunately its not happening.

Can anyone please help me how to do this?

var parser  = require("xml2js");
var xml2js = new parser.Parser();
var xhr = new XMLHttpRequest();
xhr.open("GET", "link", true);

xhr.onload = function(){
    console.log(xhr.responseText);
    xml2js.parseString(xhr.responseText,function(err, result)
    {
        console.log(result);
    });
};
xhr.send();

That’s quite hard to answer, as we don’t know what data is stored there and what you want to do with it.
But it seems like you are on the right track - you need to parse the XML (there are different packages to do that) and after parsing, it will be a js object that you can use in your code.

I am pretty sure you can ask azure tables to return json, but I can’t really say how :slight_smile:

i did the same in unity , i could use the json serializer to convert it from xml to json, but here i couldnt find, even i try multiple way its throwing an error.
Can you please tell me what is the default way to convert the xml response to json?

not entirely the purpose of this forum, but this might come in handy - jquery - Convert XML to JSON (and back) using Javascript - Stack Overflow :slight_smile:

Not mentioned on that SO link, but I can vouch for fast-xml-parser - npm (npmjs.com). I use it to convert XML to JSON - it has also a validate function. If you have well formatted XML it works well and has options for converting numbers.

1 Like

let parser = require (‘fast-xml-parser’);

when i declare this, i am getting an error on console, if remove from my code i could run and get the xml response.

any idea why its happening?

I don’t know what the error is, but it sounds like it’s not installed.

its installed and i could see the folder under the node_modules, but its not taking.

Here is the error msg.

image

require() is a Node.js function. It will not work in the browser.

The easiest way to load fast-xml-parser in the browser would be to use a script tag and point it to a CDN URL: fast-xml-parser - Libraries - cdnjs - The #1 free and open source CDN built to make life easier for developers

Googled ‘get azure table data as json’.

Set the accepts header to application/json and it’ll just return json so u dont have to do unnecessary work.

just use native fetch, you can then await response.json();

.json() transforms a json response body into an object… pretty sure it doesnt parse xml