# Request returned with XML data

**URL:** <https://community.postman.com/t/request-returned-with-xml-data/42666>\
**Category:** Help Hub\
**Tags:** variables, json, xml-response\
**Created:** [January 7, 2023, 10:07pm UTC](https://community.postman.com/t/request-returned-with-xml-data/42666 "2023-01-07T22:07:38Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![altimetry-geoscienti](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@altimetry-geoscienti](https://community.postman.com/u/altimetry-geoscienti)\
**Post date:** [January 7, 2023, 10:07pm UTC](https://community.postman.com/t/request-returned-with-xml-data/42666/1 "2023-01-07T22:07:39Z")

</div>

I’m trying to automate rating tracks in my Plex media library. I think I need to pull a list of track ids through a GET request filtered by different attributes. I’ve figured out how to do this, and am getting an XML body response. I’m using a different (small) dataset for testing. Using this code in the tests,

```auto
const jsonObject = require('xml2js');
jsonObject.parseString(pm.response.text(), function (err, result) {
    console.log(result);

```

I’m able to parse it into json (I think), which looks like this:

 ![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/1/21e1f500c880056a674eed4ff3a3703ca7b7479d.png)

and I need to figure out how to build a variable PUT\_TARGET that is comma separated values from the json. For this example let’s say I want title. So ideally, I’d get PUT\_TARGET = Movies,Tv Shows, Title3, Title4,etc…  
I’m sure this isn’t hard, but since I’m completely new, I’m really struggling. Thanks for your help.

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 8, 2023, 5:03pm UTC](https://community.postman.com/t/request-returned-with-xml-data/42666/2 "2023-01-08T17:03:14Z")

</div>

Can you post the original XML response. (Please use the Preformatted text option in the editor).

The way I would do this is to filter on the titles, which should return an array of titles, and then use a foreach to loop through the filter results to build up your variable.

---

<div class="post-metadata">

**Author:** ![altimetry-geoscienti](https://avatars.discourse-cdn.com/v4/letter/a/8dc957/32.png) [@altimetry-geoscienti](https://community.postman.com/u/altimetry-geoscienti)\
**Post date:** [January 9, 2023, 1:26am UTC](https://community.postman.com/t/request-returned-with-xml-data/42666/3 "2023-01-09T01:26:30Z")

</div>

Mike, thanks for the response. XML is below. In a conceptual sense, your suggestion sounds like an appropriate approach. Can you help me with the code to do this?

```auto
<?xml version="1.0" encoding="UTF-8"?>
<MediaContainer size="5" allowSync="0" title1="Plex Library">
    <Directory allowSync="1" art="/:/resources/movie-fanart.jpg" composite="/library/sections/2/composite/1673159120" filters="1" refreshing="0" thumb="/:/resources/movie.png" key="2" type="movie" title="Movies" agent="tv.plex.agents.movie" scanner="Plex Movie" language="en-US" uuid="b006925a-944c-490c-873c-a3ac04b08ad3" updatedAt="1667257509" createdAt="1667257509" scannedAt="1673159120" content="1" directory="1" contentChangedAt="11656615" hidden="0">
        <Location id="2" path="R:\Movies" />
        <Location id="3" path="N:\Saved_Movies" />
    </Directory>
    <Directory allowSync="1" art="/:/resources/show-fanart.jpg" composite="/library/sections/3/composite/1673159123" filters="1" refreshing="0" thumb="/:/resources/show.png" key="3" type="show" title="TV Shows" agent="tv.plex.agents.series" scanner="Plex TV Series" language="en-US" uuid="675a54c8-c603-4c65-b1b4-26fa5f0b8afe" updatedAt="1667361427" createdAt="1667257568" scannedAt="1673159123" content="1" directory="1" contentChangedAt="11982547" hidden="0">
        <Location id="4" path="R:\Television" />
        <Location id="5" path="N:\Saved_TV" />
    </Directory>
    <Directory allowSync="1" art="/:/resources/artist-fanart.jpg" composite="/library/sections/4/composite/1673158507" filters="1" refreshing="0" thumb="/:/resources/artist.png" key="4" type="artist" title="Lossless Music" agent="tv.plex.agents.music" scanner="Plex Music" language="en" uuid="355f950c-36ad-44cc-abe8-72aae9d7ade7" updatedAt="1667261516" createdAt="1667261516" scannedAt="1673158507" content="1" directory="1" contentChangedAt="6758772" hidden="0">
        <Location id="6" path="R:\Lossless Music" />
    </Directory>
    <Directory allowSync="1" art="/:/resources/artist-fanart.jpg" composite="/library/sections/6/composite/1673159123" filters="1" refreshing="0" thumb="/:/resources/artist.png" key="6" type="artist" title="Xmas Music" agent="tv.plex.agents.music" scanner="Plex Music" language="en" uuid="98d0c446-642b-477a-804d-eaffb46597a1" updatedAt="1670803453" createdAt="1670803453" scannedAt="1673159123" content="1" directory="1" contentChangedAt="7650885" hidden="0">
        <Location id="10" path="R:\Music\Christmas Music" />
    </Directory>
    <Directory allowSync="1" art="/:/resources/photo-fanart.jpg" composite="/library/sections/5/composite/1667272908" filters="1" refreshing="0" thumb="/:/resources/photo.png" key="5" type="photo" title="Photos" agent="com.plexapp.agents.none" scanner="Plex Photo Scanner" language="en" uuid="3adc677b-2c50-4310-9ab6-d607b02ea6e3" updatedAt="1667272908" createdAt="1667272908" enableAutoPhotoTags="0" content="1" directory="1" contentChangedAt="11982868" hidden="0">
        <Location id="9" path="R:\My Pictures" />
    </Directory>
</MediaContainer>

```

---

<div class="post-metadata">

**Author:** ![michaelderekjones](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/michaelderekjones/32/14819_2.png) [@michaelderekjones](https://community.postman.com/u/michaelderekjones)\
**Post date:** [January 9, 2023, 3:18pm UTC](https://community.postman.com/t/request-returned-with-xml-data/42666/4 "2023-01-09T15:18:56Z")

</div>

This seems to work.

I’m using xml2Json instead of xml2js.

```auto
var xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><MediaContainer size=\"5\" allowSync=\"0\" title1=\"Plex Library\"><Directory allowSync=\"1\" art=\"/:/resources/movie-fanart.jpg\" composite=\"/library/sections/2/composite/1673159120\" filters=\"1\" refreshing=\"0\" thumb=\"/:/resources/movie.png\" key=\"2\" type=\"movie\" title=\"Movies\" agent=\"tv.plex.agents.movie\" scanner=\"Plex Movie\" language=\"en-US\" uuid=\"b006925a-944c-490c-873c-a3ac04b08ad3\" updatedAt=\"1667257509\" createdAt=\"1667257509\" scannedAt=\"1673159120\" content=\"1\" directory=\"1\" contentChangedAt=\"11656615\" hidden=\"0\"><Location id=\"2\" path=\"R:\Movies\" /><Location id=\"3\" path=\"N:\Saved_Movies\" /></Directory><Directory allowSync=\"1\" art=\"/:/resources/show-fanart.jpg\" composite=\"/library/sections/3/composite/1673159123\" filters=\"1\" refreshing=\"0\" thumb=\"/:/resources/show.png\" key=\"3\" type=\"show\" title=\"TV Shows\" agent=\"tv.plex.agents.series\" scanner=\"Plex TV Series\" language=\"en-US\" uuid=\"675a54c8-c603-4c65-b1b4-26fa5f0b8afe\" updatedAt=\"1667361427\" createdAt=\"1667257568\" scannedAt=\"1673159123\" content=\"1\" directory=\"1\" contentChangedAt=\"11982547\" hidden=\"0\"><Location id=\"4\" path=\"R:\Television\" /><Location id=\"5\" path=\"N:\Saved_TV\" /></Directory><Directory allowSync=\"1\" art=\"/:/resources/artist-fanart.jpg\" composite=\"/library/sections/4/composite/1673158507\" filters=\"1\" refreshing=\"0\" thumb=\"/:/resources/artist.png\" key=\"4\" type=\"artist\" title=\"Lossless Music\" agent=\"tv.plex.agents.music\" scanner=\"Plex Music\" language=\"en\" uuid=\"355f950c-36ad-44cc-abe8-72aae9d7ade7\" updatedAt=\"1667261516\" createdAt=\"1667261516\" scannedAt=\"1673158507\" content=\"1\" directory=\"1\" contentChangedAt=\"6758772\" hidden=\"0\"><Location id=\"6\" path=\"R:\Lossless Music\" /></Directory><Directory allowSync=\"1\" art=\"/:/resources/artist-fanart.jpg\" composite=\"/library/sections/6/composite/1673159123\" filters=\"1\" refreshing=\"0\" thumb=\"/:/resources/artist.png\" key=\"6\" type=\"artist\" title=\"Xmas Music\" agent=\"tv.plex.agents.music\" scanner=\"Plex Music\" language=\"en\" uuid=\"98d0c446-642b-477a-804d-eaffb46597a1\" updatedAt=\"1670803453\" createdAt=\"1670803453\" scannedAt=\"1673159123\" content=\"1\" directory=\"1\" contentChangedAt=\"7650885\" hidden=\"0\"><Location id=\"10\" path=\"R:\Music\Christmas Music\" /></Directory><Directory allowSync=\"1\" art=\"/:/resources/photo-fanart.jpg\" composite=\"/library/sections/5/composite/1667272908\" filters=\"1\" refreshing=\"0\" thumb=\"/:/resources/photo.png\" key=\"5\" type=\"photo\" title=\"Photos\" agent=\"com.plexapp.agents.none\" scanner=\"Plex Photo Scanner\" language=\"en\" uuid=\"3adc677b-2c50-4310-9ab6-d607b02ea6e3\" updatedAt=\"1667272908\" createdAt=\"1667272908\" enableAutoPhotoTags=\"0\" content=\"1\" directory=\"1\" contentChangedAt=\"11982868\" hidden=\"0\"><Location id=\"9\" path=\"R:\My Pictures\" /></Directory></MediaContainer>"

// parse the XML 
var jsObject = xml2Json(xmlString);

let titles = "";

for (let i = 0; i < jsObject.MediaContainer.Directory.length; i++) {
    // console.log(jsObject.MediaContainer.Directory[i].$.title)
    titles = titles + "," + jsObject.MediaContainer.Directory[i].$.title
}

let PUT_TARGET = titles.substring(1); // removes the initial comma
console.log(PUT_TARGET);

```

![image](https://us1.discourse-cdn.com/getpostman/original/3X/2/1/211128e6f3b4fcc8d7b22a47576651fb2f2a54f1.png)

---

<div class="post-metadata">

**Author:** ![travisl12](https://sea2.discourse-cdn.com/getpostman/user_avatar/community.postman.com/travisl12/32/27185_2.png) [@travisl12](https://community.postman.com/u/travisl12)\
**Post date:** [January 20, 2024, 1:34am UTC](https://community.postman.com/t/request-returned-with-xml-data/42666/5 "2024-01-20T01:34:24Z")

</div>

If you use the header `{accept: "application/json"}` the response will be in JSON.
