Xml2js tag name has a hyphen in name

I want to read specific tag from xml body but there is a tag and there is an error:

TypeError: Cannot read properties of undefined (reading ‘hip’)

I belive the reason is that it has ‘-’ in the name.
I tried something like this but it doesn’t work.

var jsonObject = xml2Json(responseBody);
console.log(jsonObject.response.result.$["hip-report"]);

image

Hey @kubczi :wave:

Welcome to the Postman Community! :postman:

Can you share the Postman Console output with the converted JSON, please? That image looks like the response body.

Hi, that’s how it looks

I want to get down to ProductInfo and print only this part in the console but for now i stopped at . Because of the dash in the name of this tag script cant read it properly.

It looks like this is what you need:

let jsonObject = xml2Json(responseBody);
console.log(jsonObject.response.$.result["hip-report"]);

And then from there work your way to what you need.

i found the way. It appears you don’t need any . or $ but thanks

console.log(jsonObject.response.result[“hip-report”]

1 Like

xml2js is also available in the Postman sandbox and gives you a lot more control over the output.

For example…

let respBodyXML = pm.response.text();

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

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

This produces output like the following. It removes the $ signs, and looks more like a proper JSON object.

image

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

2 Likes

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