4.0

4.0 | 1 rating Rate this file 57 downloads (last 30 days) File Size: 2.4 KB File ID: #20565

JSON Parser

by Joel Feenstra

 

03 Jul 2008 (Updated 18 Jun 2009)

Code covered by BSD License  

Parses JSON strings into structures and cells

Download Now | Watch this File

File Information
Description

This function parses JSON strings. It converts JSON arrays into cell arrays and JSON objects into structures.

This can be used with webervices that return JSON data such as the API provided by Google®.

An example of use is:
  google_search = 'http://ajax.googleapis.com/ajax/services/search/web?v=1.0&q=matlab';
  matlab_results = parse_json(urlread(google_search));
  disp(matlab_results{1}.responseData.results{1}.titleNoFormatting)
  disp(matlab_results{1}.responseData.results{1}.visibleUrl)

Acknowledgements
This submission has inspired the following:
(another) JSON Parser , Highly portable JSON-input parser
MATLAB release MATLAB 7.5 (R2007b)
Zip File Content  
Other Files license.txt,
parse_json.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (6)
28 Nov 2008 port tracker

Good work!
BTW - Any reason why it only return 4 value object from google? Is it because of the ajax api?

Thanks,

port

08 Dec 2008 Joel Feenstra

The Google API limits it to 4 results. You can use the "cursor" information to fetch more results: matlab_results{1}.responseData.cursor

16 Mar 2009 Patrick Boert

Hallo Joel,

thanks for the work!

I'm trying to query the Chruchbase API and it seems to work fine. However, it takes ages for some queries to get parsed, e.g. I quit

companies=parse_json(urlread('http://api.crunchbase.com/v/1/companies.js'));

after 1 hour. The profiler says that

json(1) = []; in parse_json>parse_string
json(1) = []; in parse_json>parse_object

make up about 90 % of the computational cost. Is there some easy way around this?

Best,
Patrick

23 Mar 2009 François Glineur

An alternative parser with faster handling of strings is available at http://www.mathworks.com/matlabcentral/fileexchange/23393
(another JSON Parser). For example, running the example above

companies=parse_json(urlread('http://api.crunchbase.com/v/1/companies.js'));

with it now takes less than 30 seconds.

23 Mar 2009 Joel Feenstra

I know that the code is slow, and I made no attempt to make the code fast, just correct. François's version does seem to be faster, so if you need speed (and have a lot of strings), use his.

23 Mar 2009 Patrick Boert

Thanks Joel, I thought maybe there would be an easy workaround you could point me to. But the new file solves the issue, it is faster by some magnitude. In any case, thanks for your work.

Please login to add a comment or rating.
Updates
18 Jun 2009

Changed the description to properly reference Google®.

Tag Activity for this File
Tag Applied By Date/Time
data import Joel Feenstra 22 Oct 2008 10:08:23
data export Joel Feenstra 22 Oct 2008 10:08:23
json Joel Feenstra 22 Oct 2008 10:08:23
webservice Joel Feenstra 22 Oct 2008 10:08:23
strings Joel Feenstra 22 Oct 2008 10:08:23
parses Joel Feenstra 22 Oct 2008 10:08:23
converts Joel Feenstra 22 Oct 2008 10:08:23
arrays Joel Feenstra 22 Oct 2008 10:08:23
cell arrays Joel Feenstra 22 Oct 2008 10:08:23
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com