5.0

5.0 | 3 ratings Rate this file 98 downloads (last 30 days) File Size: 2.76 KB File ID: #23393

(another) JSON Parser

by François Glineur

 

22 Mar 2009 (Updated 23 Jul 2009)

Code covered by BSD License  

Parses JSON strings into structures and cells.

Download Now | 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)
Zip File Content  
Other Files license.txt,
parse_json.m
Tags for This File  
Everyone's Tags
Tags I've Applied
Add New Tags Please login to tag files.
Comments and Ratings (12)
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

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

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
cells François Glineur 22 Mar 2009 21:29:59
cell arrays François Glineur 22 Mar 2009 21:29:59
 

MATLAB Central Terms of Use

NOTICE: Any content you submit to MATLAB Central, including personal information, is not subject to the protections which may be afforded information collected under other sections of The MathWorks, Inc. Web site. You are entirely responsible for all content that you upload, post, e-mail, transmit or otherwise make available via MATLAB Central. The MathWorks does not control the content posted by visitors to MATLAB Central and, does not guarantee the accuracy, integrity, or quality of such content. Under no circumstances will The MathWorks be liable in any way for any content not authored by The MathWorks, or any loss or damage of any kind incurred as a result of the use of any content posted, e-mailed, transmitted or otherwise made available via MATLAB Central. Read the complete Terms prior to use.

Contact us at files@mathworks.com