1

I have this output from urlread function:

// [
{
"id": "22144"
,"t" : "AAPL"
,"e" : "NASDAQ"
,"l" : "148.59"
,"l_fix" : "148.59"
,"l_cur" : "148.59"
,"s": "0"
,"ltt":"1:13PM EDT"
,"lt" : "May 5, 1:13PM EDT"
,"lt_dts" : "2017-05-05T13:13:23Z"
,"c" : "+2.06"
,"c_fix" : "2.06"
,"cp" : "1.41"
,"cp_fix" : "1.41"
,"ccol" : "chg"
,"pcls_fix" : "146.53"
,"eo" : ""
,"delay": ""
,"op" : "146.76"
,"hi" : "148.91"
,"lo" : "146.76"
,"vo" : "-"
,"avvo" : "-"
,"hi52" : "148.91"
,"lo52" : "89.47"
,"mc" : "771.93B"
,"pe" : "17.38"
,"fwpe" : ""
,"beta" : "1.21"
,"eps" : "8.55"
,"shares" : "5.21B"
,"inst_own" : "63%"
,"name" : "Apple Inc."
,"type" : "Company"
}
]

My question is how can I convert this to a two-column cell? Or even better create a structure called AAPL which gives me , for example for AAPL.l the price?

user27103
  • 13
  • 2
  • Is that whole thing a string now? If it is then check out jsondecode function. – Navan May 05 '17 at 17:24
  • I tried it gives me the following error " JSON syntax error at line 2, column 1 (character 2): expected value but found '/'. " . The when I delete the first lines (the ones which give me that error) the problem is " JSON syntax error at line 1, column 5 (character 5): extra text. " – user27103 May 05 '17 at 17:54
  • If the string starts from '{' it should work fine. For example try the smaller subset, jsondecode('{"id": "22144","t" : "AAPL","e" : "NASDAQ","l" : "148.59"}') – Navan May 05 '17 at 18:05
  • Thank you so much! It works. – user27103 May 05 '17 at 18:15
  • Sure. I posted this as an answer. – Navan May 05 '17 at 18:37

1 Answers1

1

Use jsondecode function to convert JSON format text to a MATLAB struct type. Typically the text would start with a '[' or '{'. You can try code using a simpler subset as below.

jsondecode('{"id": "22144","t" : "AAPL","e" : "NASDAQ","l" : "148.59"}')

This produces a struct with the following fields.

id: '22144'
 t: 'AAPL'
 e: 'NASDAQ'
 l: '148.59'
Navan
  • 4,407
  • 1
  • 24
  • 26