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.

1 Like

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

1 Like

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));
5 Likes

Works perfect. Thank you @arogers840 !

Hi,
Thank you for this snipplet, it allows me to display the response in the console in json format (removing the last line with the deprecated xml2Json).
I am struggling to understand how I can access a particular element of the json object, e.g. the “CoverType” from the above example. I just can’t make it work. Would you be able to provide a line that displays the CoverType in the console? Thank you!

@colua

Can you provide an example of the XML being produced, and then the code you currently have to create the JavaScript\JSON object.

This will then allow us to show you how to target the relevant elements.

Hi. Thanks for trying to help. I finally made it work, just after I posted the message… typical. I had a typo in my code (mixup between capital i and small L…), my bad! Additional debugging helped. Sorry for the unnecessary question after all :-/

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.