View License

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

» Watch video

Highlights from
JSON Parser

3.9 | 17 ratings Rate this file 180 Downloads (last 30 days) File Size: 2.4 KB File ID: #20565 Version:

JSON Parser


Joel Feenstra (view profile)


03 Jul 2008 (Updated )

Parses JSON strings into structures and cells

| Watch this File

File Information

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 = '';
  matlab_results = parse_json(urlread(google_search));


This file inspired Twitty, Exchangerate.M, Jso Nlab: A Toolbox To Encode/Decode Json Files, Highly Portable Json Input Parser, (Another) Json Parser, Matlab Pushbullet, and Core Jsonparser: Import And Export Json Files Using Matlab.

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 (27)
01 Nov 2016 Eric

Eric (view profile)

26 Aug 2016 witzgf

witzgf (view profile)


18 Jul 2016 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.

Comment only
09 Mar 2016 Jennifer Bauman

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


Comment only
07 Jan 2016 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],[]]}.

27 Oct 2015 Sanjay Manohar

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

10 Sep 2015 Srdjan

Srdjan (view profile)

Crashes with BeaqleJS-generated JSON files.

13 Oct 2014 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

10 Jul 2014 Alceu Costa

Alceu Costa (view profile)

04 Jul 2014 Michael Weidman

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

25 Mar 2014 Adeola Bannis

07 Oct 2013 Yohai

Yohai (view profile)

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

For example, try


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)

Comment only
28 Jun 2013 Suresh Deoda


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

Qianqian Fang (view profile)

  • 1 file
  • 4.49091

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

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

Comment only
22 Nov 2011 Boyang Gao

This version cannot deal with JSON file with empty elements. Try 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.

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

Comment only
23 Mar 2009 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.

Comment only
23 Mar 2009 François Glineur

An alternative parser with faster handling of strings is available at
(another JSON Parser). For example, running the example above


with it now takes less than 30 seconds.

Comment only
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


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?


Comment only
08 Dec 2008 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

Comment only
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?



18 Jun 2009 1.2

Changed the description to properly reference Google®.

01 Sep 2016

Updated license

Contact us