4.11111

4.1 | 9 ratings Rate this file 168 Downloads (last 30 days) File Size: 2.4 KB File ID: #20565

JSON Parser

by

 

03 Jul 2008 (Updated )

Parses JSON strings into structures and cells

| 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 file inspired Twitty, Jso Nlab: A Toolbox To Encode/Decode Json Files In Matlab/Octave, Highly Portable Json Input Parser, and (Another) Json Parser.

MATLAB release MATLAB 7.5 (R2007b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (17)
25 Mar 2014 Adeola Bannis  
07 Oct 2013 Yohai

does not handle improper json files, and can be stuck in an infinite loop.

For example, try

p=parse_json('"a":1')

01 Jul 2013 ashish dabral

Hi, I am using this parser for parsing a JSON containing a base64string of an image. Apparently, if the base64 is smaller than 100kb, the image is parsed easily but if the image string is big, say, from an image around 1mb size, matlab takes forever to process it. Could it be json parser or is it urlread that's slowing down the process. I just get a busy message(waiting for over an hour for my image to get procesed)

28 Jun 2013 Suresh Deoda

Thanks

08 Oct 2012 John Reinert Nash

Very useful tool, works as described. Thank you for simplifying parsing web API returned data.

15 Jan 2012 Qianqian Fang

FYI, an optimized JSON parser, JSONlab, based on this work is available at

http://www.mathworks.com/matlabcentral/fileexchange/33381

It is 10x to 100x faster. A JSON encoder is also provided in JSONlab.

22 Nov 2011 Boyang Gao

This version cannot deal with JSON file with empty elements. Try http://www.mathworks.com/matlabcentral/fileexchange/23393 instead(also faster).

10 Jan 2011 Benjamin Ting

This code does not appear to work if the first { is preceeded by white space (either a space or carriage return).

03 Dec 2010 Dan Massie

Works well, despite being a bit slow. Maybe it could be sped up with careful application of "regular expressions." Also, when you use the script, make sure the JSON string is a row vector of chars. Error may occur if string is a column vector of chars.

08 May 2010 Ashish Uthama  
22 Feb 2010 Han Oostdijk

Thanks Joel,

just what I needed and it works fine.

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.

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 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.

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

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

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

Updates
18 Jun 2009

Changed the description to properly reference Google®.

Contact us