Issues converting a string containing xml to json

Ah OK, now I understand what you’re trying to do, thanks!

The library that I’d normally use for this is xml2js; I’d welcome somebody showing me a simpler solution, but in the meantime, this should do what you’re hoping for:

var parseString = require('xml2js').parseString;
parseString(the_piece_xml_string, function (err, result) {
    pm.collectionVariables.set("the_piece", JSON.stringify(result));
});

:tada: Ta-da:

xml2js has other configuration options if you want to more precisely control the output format (which may be required depending on your ‘real’ XML structure) and it’s definitely worth including some error-handling in case the XML data is returned in an unexpected/invalid format, but hopefully this helps you get to the next stage?

1 Like