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,
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:
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.
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.
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?
<?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>
This seems to work.
I’m using xml2Json instead of xml2js.
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);
travisl12
(travisl12)
January 20, 2024, 1:34am
5
If you use the header {accept: "application/json"}
the response will be in JSON.