Kyle Burriss Kyle Burriss - 1 month ago 32
JSON Question

How do I extract a variable from XML using Postman?

I'm trying to extract a SessionId from the XML which is returned from a SOAP API.

I've read through the Postman documentation (several times over) but it wasn't the most helpful in achieving my goal.

What was suggested in a few blogs was to convert the XML to JSON, and then pick out the token and it's value from there, but that didn't help either.

I used the following in my Test:

var jsonObject = xml2Json(responseBody);
postman.setGlobalVariable("Session_Id", jsonObject.SessionID);


The above created the variable "Session_Id" but didn't actually assign a value to it. I'm stumped.

I'm definitely retrieving the data from the API, and it's viewable in Postman's "Body" Response.

Answer

To extract a variable from XML using Postman, first convert your XML to JSON, using the xml2Json converter method:

var responseJson = xml2Json(responseBody);

(Where "responseBody" is your xml body.) Then use the console.log method to output your JSON data, as such:

console.log(responseJson);

Be sure to have followed this tutorial on Enabling Chrome Dev Tools in Postman

Inside your Test Runner, run the test, then right click and "Inspect" anywhere in the Runner. Select the "Console" tab once Chrome's Dev Tools launch. Expand the "Object" part.

Then drill-down (expand) until you see the Property whose data you need. Thereafter, set the variable by appending each drill-down level to the parameter you want:

postman.setGlobalVariable("Session_Id", responseJson.UserSessionToken.SessionID); 

In this case, "responseJson" is the object, "UserSessionToken" was the next level in the drill-down, and SessionId was the parameter I needed within that.