3
$\begingroup$

Having a bit of trouble pulling a value out of a JSON Association here. I am scraping this website: http://www.msn.com/en-us/money/stockdetails/history/fi-126.1.BY.NYS

I want to scrape the closing stock prices for everyday, and using developer tools found the JSON link that stores the data, with the link: http://finance.services.appex.bing.com/Market.svc/ChartDataV5?symbols=\ 126.1.BY.NYS&chartType=1y&isEOD=False&lang=en-US&isCS=true&isVol=true

I've gotten rather close, and have narrowed down the association and keys using this:

res1 = Association[
   Import["http://finance.services.appex.bing.com/Market.svc/\
ChartDataV5?symbols=126.1.BY.NYS&chartType=1y&isEOD=False&lang=en-US&\
isCS=true&isVol=true", "JSON"]];
Keys[res1];
data = res1["Series"];
Keys[data];
content = "p" /. data

This block of code essentially returns that the values I am seeking are under the "p" Key, for instance:

{"T" -> 9201600, "P" -> 20.05, "Hp" -> 20.48, "Lp" -> 19.84, 
 "Op" -> 20.35, "V" -> 3492724}

I want to extract the 20.05 in "p" -> 20.05. There are several "p" values in data, and ideally I would grab all of them and put them into a list.

$\endgroup$
  • $\begingroup$ Replace p with P? content = "P" /. data $\endgroup$ – C. E. Jul 11 '17 at 23:43
  • $\begingroup$ Sometimes I am surprised by my own lack of attention to detail. Thank you! $\endgroup$ – Vic Jongmin Youn Jul 12 '17 at 14:39
4
$\begingroup$

First of all, I think you can make your life a bit easier by using RawJSON as input format specification. It basically makes Mathematica use Associations where appropriate when importing JSON.

url = "http://finance.services.appex.bing.com/Market.svc/ChartDataV5?symbols=126.1.BY.NYS&chartType=1y&isEOD=False&lang=en-US&isCS=true&isVol=true";
import = Import[url, "RawJSON"]

Try the request with both RawJSON and JSON for yourself. Since our data now consists of lists and associations, we can conveniently process it using Part. This should accomplish what you were asking for:

Part[import, 1, "Series", All, "P"]
{20.05, 20.1, 20.2, 20.18, 20.27, 20.35, 20.3}
$\endgroup$
  • $\begingroup$ That definitely makes things easier. Thank you! $\endgroup$ – Vic Jongmin Youn Jul 12 '17 at 14:39

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.