# Attempting to save variable from XML to no avail

**URL:** <https://community.postman.com/t/attempting-to-save-variable-from-xml-to-no-avail/30295>\
**Category:** Help Hub\
**Tags:** variables, xml\
**Created:** [December 2, 2021, 6:08am UTC](https://community.postman.com/t/attempting-to-save-variable-from-xml-to-no-avail/30295 "2021-12-02T06:08:53Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![dmag](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@dmag](https://community.postman.com/u/dmag)\
**Post date:** [December 2, 2021, 6:08am UTC](https://community.postman.com/t/attempting-to-save-variable-from-xml-to-no-avail/30295/1 "2021-12-02T06:08:53Z")

</div>

Firstly, being complete up front, I am a novice when it comes to utilising APIs (only started playing around a couple of weeks ago).

The APIs that I need to use regularly require a session key, which expires pretty fast if you are not actively using, and so I was hoping to store that key as a variable in my collection so I could build out calls that are regularly used and put the variable in to save copy/paste time.

However, I don’t seem to be able to get the variable to save in the correct format. I have attached a picture below to show what this looks like but basically the response for the session key request is;

```auto
<response status="success">
   <value type="string">4whOJWwMi1OfXRybTzWn7ANDZi43Vee3CCfqJY9cUUk.</value>
</response>

```

I have attempted to pop that into a variable by first converting to JSON object, then pulling out the session key and finally saving it as a variable. However, the script below doesn’t seem to take the value on its own and save it as a string that I can reuse but rather stores it as some kind of object.

```auto
var jsonObject = xml2Json(responseBody);
var key = jsonObject.response.value;
pm.collectionVariables.set("dans-session-key", jsonObject.response.value);

```

Kinda out of my depth here and would appreciate a steer.

 ![Example](https://us1.discourse-cdn.com/getpostman/original/2X/5/52420c4921092a19f4d6735addf5bde576019192.png)

---

<div class="post-metadata">

**Author:** ![w4dd325](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/w4dd325/32/12436_2.png) [@w4dd325](https://community.postman.com/u/w4dd325)\
**Post date:** [February 8, 2022, 10:32am UTC](https://community.postman.com/t/attempting-to-save-variable-from-xml-to-no-avail/30295/2 "2022-02-08T10:32:44Z")

</div>

Hi,  
Try this;

```auto
//Parse
var jsonObject = xml2Json(responseBody);
console.log(jsonObject);

//Locate value
var key = jsonObject.response.value;
console.log("key = ", key);

//Get and Set
pm.collectionVariables.set("DansSessionKey", key['_']);
console.log("DansSessionKey = ", pm.collectionVariables.get("DansSessionKey"));

```

 ![image](https://us1.discourse-cdn.com/getpostman/original/2X/b/be705396d2a0a2271c36a4c56520854475e2ae67.png)

---

<div class="post-metadata">

**Author:** ![dmag](https://avatars.discourse-cdn.com/v4/letter/d/3ab097/32.png) [@dmag](https://community.postman.com/u/dmag)\
**Post date:** [February 9, 2022, 1:08am UTC](https://community.postman.com/t/attempting-to-save-variable-from-xml-to-no-avail/30295/3 "2022-02-09T01:08:18Z")

</div>

Thanks for replying, I actually managed to figure this out by adding .\_ when grabbing the value out as I didn’t realise but the ‘value’ attribute had two children (being ‘\_’ and ‘$’).

var jsonObject = xml2Json(responseBody);  
pm.collectionVariables.set(“dans-session-key”, jsonObject.response.value.\_);
