Code covered by the BSD License  

Highlights from
(another) JSON Parser

4.83333

4.8 | 6 ratings Rate this file 60 Downloads (last 30 days) File Size: 2.76 KB File ID: #23393

(another) JSON Parser

by François Glineur

 

22 Mar 2009 (Updated 11 Jan 2011)

Parses JSON strings into structures and cell arrays.

| Watch this File

File Information
Description

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

It is similar to the JSON parser by Joel Feenstra but often faster because of a better handling of strings.

Acknowledgements

The author wishes to acknowledge the following in the creation of this submission:
JSON Parser
This submission has inspired the following:
Google(R) Translate, Highly portable JSON-input parser

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (19)
23 Mar 2009 Joel Feenstra

It is definitely faster than my version.

23 Mar 2009 Patrick Boert

Thanks a lot François, this is exactly what I was looking for.

24 Mar 2009 Patrick Boert  
04 May 2009 Michael Katz

Good program, I made use of it in my google translate tool: http://www.mathworks.com/matlabcentral/fileexchange/23988

(i forgot to include the file in my first submission)

20 Jul 2009 Rob Newman

I have a JSON file local to my Matlab script. I fopen() and fread() the JSON file, but (another) JSON Parser crashes with the following error:

??? Undefined function or method 'regexp' for input arguments of type 'double'.

Error in ==> parse_json at 15
esc = regexp(string, '["\\]'); index_esc = 1; len_esc = length(esc);

Error in ==> ta_data_return at 35
file_sta_list = parse_json( json_file_obj ) ;

I checked the validity of the JSON source file at http://www.jsonlint.com/ and it is valid. Any ideas? Thanks in advance.

21 Jul 2009 François Glineur

Rob, have you checked that the argument you pass to parse_json is a string ? The easiest way to convert the contents of a file into a string is to use the function fileread, as in this example :

s = fileread('file.json');
res = parse_json(s);

21 Jul 2009 Rob Newman

Thanks François - that helped! I was using fopen() and fread(). The fileread() function worked, to a point. Now I get an error:

??? Invalid field name: '526A'.

Error in ==> parse_json>parse_object at 39
                object.(str) = val;

Here is a snippet of my JSON file, which does validate, as noted above:

{
    "active":{
        "E25A": {
            "commtype":"cell modem",
            "snet":"TA",
            "provider":"verizon"
        },
        "TIGA":{
            "commtype":"cell modem",
            "snet":"TA",
            "provider":"verizon"
        },
        "526A":{
            "commtype":"cell modem",
            "snet":"TA",
            "provider":"verizon"
        }
    }
}

Note that the JSON file in its entirety is 200KB, so maybe there is an issue with how large a file your script can handle? Or possibly how 'deep' the object (i.e. how many levels of '{}') in the JSON structure there is?

21 Jul 2009 François Glineur

Rob, the problem is caused by field "526A", which MATLAB cannot accept because field names in MATLAB must start with a letter. I will update the parser to convert invalid field names into acceptable ones.

22 Jul 2009 Rob Newman

I mean a real limitation in Matlab - NOT your script :)

22 Jul 2009 Rob Newman

And thanks again!

23 Jul 2009 Rob Newman

François, I just downloaded the latest version and the problem still persists. Also, the file change time for parse_json.m is May 19, 2009 which is the same as the original (I think).

24 Jul 2009 naini naveen

thanks again

12 Feb 2010 Thomas Smith

Hi, does anyone know if there's a matlab script to *generate* JSON? It would be great if I could do both input and output... Otherwise I might have to use XML

10 Jan 2011 Benjamin Ting

The comment box indicates the function returns a cell array. However, the return type is not a cell.

11 Jan 2011 François Glineur

Dear Benjamin: you are right, the function returns a cell array only if the topmost entity in the JSON string is an array ; if it is instead an object, the function returns a structure. I have updated the description to reflect that.

25 Feb 2011 Eric

Works like a charm on WeatherBug REST JSON API output of hourly weather forecasts.

06 Jul 2011 Jim Hokanson  
06 Jul 2011 Jim Hokanson

I love the use of nested functions! Well written.

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.

Please login to add a comment or rating.
Updates
21 Jul 2009

If needed, fields names are modified to make them compatible with MATLAB's requirements (only letters, digits and underscores, first character is a letter)

23 Jul 2009

Contains previous update that was incorrectly uploaded

11 Jan 2011

Updated description to take into account remark by Benjamin Ting

Tag Activity for this File
Tag Applied By Date/Time
json François Glineur 22 Mar 2009 21:29:59
parser François Glineur 22 Mar 2009 21:29:59
javascript François Glineur 22 Mar 2009 21:29:59
cell arrays François Glineur 22 Mar 2009 21:29:59
cells François Glineur 22 Mar 2009 21:29:59
json Jan Tore Korneliussen 29 Apr 2010 14:27:50
data import François Glineur 12 Jan 2011 07:07:00

Contact us at files@mathworks.com