I am trying to pull out a certain value from an XML tree that I have acquired from a website:
url = "http://store.nike.com/us/en_us/pd/dry-knit-mens-short-sleeve-running-top/pid-11194817/pgid-11463524";
xmlLarge = Import[url, "XMLObject"];
firstCase = Cases[xmlLarge,
XMLElement[
"script", {"id" -> "product-data", "type" -> "template-data"}, {___}], Infinity]
The code above returns a fragment of the XML tree that looks something like this:
{XMLElement[
"script", {"id" -> "product-data", "type" -> "template-data"}, {"
{\"productId\":\"11194817\",\"productGroupId\":\"\
11463524\",\"displayName\":\"Nike Dry Knit Men's Short Sleeve Running \
Top\",\"productTitle\":\"Nike Dry Knit\",\"productSubTitle\":\"Men's \
Short Sleeve Running Top\",\"pageTitle\":\"Nike Dry Knit Men's Short \
Sleeve Running Top. \
Nike.com\",\"catalogId\":\"1\",\"productSlug\":\"dry-knit-mens-short-\
sleeve-running-top\",\"url\":\"http://store.nike.com/us/en_us/pd/dry-\
knit-mens-short-sleeve-running-top/pid-11194817/pgid-11463524\",\"\
startDate\":1481875200000,\"builderUrl\":null,\"frameUtilUrl\":null,\"\
hardLaunch\":false,\"reviews\":{\"averageRating\":4.5,\"\
formattedAverageRating\":\"4.5\",\"ratingRange\":5,\"totalReviewCount\
\":4,"
I have copy and pasted only a segment of the XML tree to give a sense of its structure. The code above seems to be returning certain variables and their values ... i.e. "productTitle":"Nike Dry Knit"
. My goal is to extract the value given the name of the variable, specifically "totalReviewCount"
, which would return 4
. In the Documentation, it says that :
means pattern, but the set up also reminds me of Keys
, so I am a bit confused. Any help and guidance would be greatly appreciated!
ImportString[...,"JSON"]
fails for some reason. $\endgroup$XMLElement
) is just an unparsed string... $\endgroup$ImportString[firstCase[[1, 3,1]], "JSON"]
does not work, but if one copies it into a JSON validator then it works. And then if you copy it back it also works in Mathematica. Maybe there are hidden characters that get stripped in the copying. $\endgroup$ImportString["\"\:2019\"", "RawJSON"]
. $\endgroup$