File Exchange

image thumbnail

JSON Parser

version 1.2.0.1 (2.4 KB) by

Parses JSON strings into structures and cells

3.88235
17 Ratings

154 Downloads

Updated

View License

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)

Comments and Ratings (27)

Eric

Eric (view profile)

witzgf

witzgf (view profile)

good

Nicholas Mati

As indicated by Joel Feenstra, this code is not optimized for speed. There's a large amount of data copying going on with n^2 time complexity. Profiling, one of the main offenders is the instruction

  json(1) = [];

which ends up copying the entire JSON string into a new block of memory minus the first character. There are similar instances of this in other portions of the code. I would highly recommend against using this implementation on strings longer than 10^5 characters. Otherwise, I didn't encounter any issues.

How do I use this file if I have a .json file saved with all the data I want to convert to .mat?

Thanks!

Falk Lieder

This function is incredibly useful. It saved me a lot of time! Thank you so much!

I noticed that the current implementation breaks if the JSON contains an empty array inside another array, as in "experiment={"name" : value, "array": [[1,2,3],[]]}.

Great. However, this baulks at names beginning with an underscore.

Srdjan

Srdjan (view profile)

Crashes with BeaqleJS-generated JSON files.

Matlab2010

I get the occasional issue:

Warning: Control Character '\%' is not valid. See 'doc sprintf' for control characters valid in the format string.
> In parse_json>parse_string at 151
  In parse_json>parse_value at 31
  In parse_json>parse_name_value at 128
  In parse_json>parse_object at 102
  In parse_json>parse_value at 34
  In parse_json at 16
  In twitty>twitty.parseTwitterResponse at 434
  In twitty>twitty.callTwitterAPI at 2014
  In twitty>twitty.friendshipsCreate at 1211

Alceu Costa

Alceu Costa (view profile)

Does precisely what it claims: perfect for me to quickly parse results from the Google Geocoding API with no headaches. Thanks, Joel!

Adeola Bannis

Yohai

Yohai (view profile)

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

For example, try

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

 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)

Suresh Deoda

Thanks

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

Qianqian Fang

Qianqian Fang (view profile)

  • 1 file
  • 614 downloads
  • 4.55102

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.

Boyang Gao

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

Benjamin Ting

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

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.

Ashish Uthama

Han Oostdijk

Thanks Joel,

just what I needed and it works fine.

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.

Joel Feenstra

Joel Feenstra (view profile)

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.

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.

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

Joel Feenstra

Joel Feenstra (view profile)

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

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

1.2.0.1

Updated license

1.2

Changed the description to properly reference Google®.

MATLAB Release
MATLAB 7.5 (R2007b)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video