kubczi
(kubczi)
March 7, 2024, 9:03am
1
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"]);
Hey @kubczi
Welcome to the Postman Community!
Can you share the Postman Console output with the converted JSON, please? That image looks like the response body.
kubczi
(kubczi)
March 7, 2024, 9:20am
3
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.
kubczi
(kubczi)
March 7, 2024, 9:49am
5
Danny Dainton:
let jsonObject = xml2Json(responseBody);
console.log(jsonObject.response.$.result["hip-report"]);
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.
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 - Help - Postman Community
2 Likes
system
(system)
Closed
March 10, 2024, 11:35am
7
This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.