File Exchange

image thumbnail

JSON Parser

version (2.4 KB) by Joel Feenstra
Parses JSON strings into structures and cells


Updated 01 Sep 2016

View Version History

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

Cite As

Joel Feenstra (2021). JSON Parser (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (34)


David Fardis

Hello ... This one is nice ... but it's easy to work with online.

Racks Jeen

Hello Friends,

Nice Tool... But if you want to use it for a single time then directly use <a href=""></a> this tool.


Oliver Faust

Dear Joel,

Please consider providing a list of modified names. See below for a dirty hack which provides that list.
function [data json] = parse_object(json)
global modifiedList
data = [];
while ~isempty(json)
id = json(1);
json(1) = [];

switch id
case '"' % Start a name/value pair
[name value remaining_json] = parse_name_value(json);
if isempty(name)
ME = MException('json:parse_object',['Can not have an empty name: ' json]);
[N, modified] = matlab.lang.makeValidName(name);
data.(N) = value;
if modified
modifiedList{end+1}={N name}
json = remaining_json;

case '}' % End of object, so exit the function

otherwise % Ignore other characters

Orlando Tovar

Iris Panabaker

Loved this tool and would love to suggest another tools which will be helpful.




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.

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?


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],[]]}.

Sanjay Manohar

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


Crashes with BeaqleJS-generated JSON files.


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

Michael Weidman

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

Adeola Bannis


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

For example, try


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)

Suresh Deoda


John Reinert Nash

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

Qianqian Fang

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.

Boyang Gao

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

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
(another JSON Parser). For example, running the example above


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


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?


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

port tracker

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



MATLAB Release Compatibility
Created with R2007b
Compatible with any release
Platform Compatibility
Windows macOS Linux

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!