Occational warning that xml2Json is deptrecated

We have been using xml2Json for a while. As of this week, we are getting ‘occasional’ console errors stating it has been deprecated and to use ‘require(‘xml2js’)’ instead.

On doing this, the format that is created is not as we had before - I am sure there is something really simple we are missing

This is from xml2Json
image

This is from xml2js
image

We obviously would like it to have remained the same so our tests do not need rework.

Any assistance is appreciated.

xml2Json has a lot of formatting options.

For example, try the following…

let respBodyXML = pm.response.text();

let parseString = require('xml2js').parseString;

parseString(respBodyXML, {
    ignoreAttrs: true, 
    explicitArray: false,
}, function (err, result) {
    console.log(result);
});

It depends on how complex your XML is, but have a look here (scroll to the bottom) for all of the formatting options.

xml2js - npm (npmjs.com)

Have a look here for another topic that shows some of the formatting options in action.

How to retrieve the names of xml tags - :person_raising_hand: Help - Postman Community

So, trying literally every option, I am still not able to get the JSON in the previously tested format.

I guess for now a ‘deprecation’ warning will have to be managed.

Can you share an example response of the original XML?

Cut and paste using the preformatted text option in the forum editor.

Can you also share a full example of what xml2Json was producing?

There are tons of options for that library\method. I’ll have a go at replicating what you currently have.

I had essentially the same issue and was able to fix it by setting the explicitArray flag to false, similar to what the previous comment mentioned. It should remove the array structures and 0’s from the json object. There may be other differences but I haven’t came across any in the XML responses I’ve worked with recently.

const xml2js = require('xml2js');
let jsonResponse;
xml2js.parseString(pm.response.text(),{explicitArray: false}, (err, result)=>{
    jsonResponse = result;
});
console.log(jsonResponse);
console.log(xml2Json(responseBody));
3 Likes

Works perfect. Thank you @arogers840 !