Code covered by the BSD License  

Highlights from
(another) JSON Parser

4.875
4.9 | 8 ratings Rate this file 101 Downloads (last 30 days) File Size: 2.76 KB File ID: #23393 Version: 1.4

(another) JSON Parser

by

 

22 Mar 2009 (Updated )

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

Json Parser inspired this file.

This file inspired Google(R) Translate, Jso Nlab: A Toolbox To Encode/Decode Json Files In Matlab/Octave, and Highly Portable Json Input Parser.

MATLAB release MATLAB 7.7 (R2008b)
Tags for This File   Please login to tag files.
Please login to add a comment or rating.
Comments and Ratings (25)
11 Nov 2014 Brendan Hannigan

fast!

13 May 2014 jason

jason (view profile)

hello Francois! I have used your program to parse my json file and it succeeded! thank your for your efforts!

but when I try to use parse_json to parse a much larger json file which is 3.38G errors occurred. I can't understand the errors, can you help me? follow is the error message:

Error using parse_json/error_pos (line 191)
Expected , at position 285210628 : ... " : "闂佃10璺?,
"<error>BaiduLongitute" : 121 ...

Error in parse_json/parse_char (line 68)
error_pos(sprintf('Expected %c at position %%d', c));

Error in parse_json/parse_object (line 43)
parse_char(',');

Error in parse_json/parse_value (line 157)
val = parse_object;

Error in parse_json/parse_array (line 54)
val = parse_value;

Error in parse_json (line 22)
data = parse_array;

Error in ProgrameForBus_position (line 2)
data=parse_json(string)

and I firstly use fileread function to transfer json to string, the string had been successfully obtained.

Comment only
04 Feb 2014 ciao

ciao (view profile)

Hi! I parsed this url:
url = 'http://api.wunderground.com/api/ea9003517a31d389/hourly10day/settings/q/IT/Venezia.json';
contents = urlread(url);
data = parse_json(contents);

How can I transform the structure data in arrays with the ourly temperature/humidity ext data?

Thanks in advance
davide

Comment only
30 Jun 2013 Oscar

Oscar (view profile)

This worked great. (better than the original parser by Feenstra)

01 Jun 2013 François Glineur

Hi Karamos,
This code is designed to parse JSON strings ; in your example it is not clear where the JSON string to be parsed is supposed to come from. Also note this code should be called with a string, so "cellstr" is not necessary.

Comment only
29 May 2013 Karamos

Could you please give me an indication how to convert this string into cell:

fullURL = ['http://www.fxstreet.com/'];
matlab_results = parse_json(cellstr(urlread(fullURL)))

I have the following error

??? SWITCH expression must be a scalar or string constant.

Error in ==> parse_json_V1 at 18
switch(next_char)

thanks

Comment only
15 Jan 2012 Qianqian Fang

Qianqian Fang (view profile)

  • 1 file
  • 491 downloads
  • 4.37037

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.

Comment only
06 Jul 2011 Jim Hokanson

Jim Hokanson (view profile)

I love the use of nested functions! Well written.

Comment only
06 Jul 2011 Jim Hokanson

Jim Hokanson (view profile)

 
25 Feb 2011 Eric

Eric (view profile)

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

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.

Comment only
10 Jan 2011 Benjamin Ting

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

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

Comment only
24 Jul 2009 naini naveen

thanks again

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

Comment only
22 Jul 2009 Rob Newman

And thanks again!

Comment only
22 Jul 2009 Rob Newman

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

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.

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

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

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

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

24 Mar 2009 Patrick Boert  
23 Mar 2009 Patrick Boert

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

Comment only
23 Mar 2009 Joel Feenstra

Joel Feenstra (view profile)

It is definitely faster than my version.

Comment only
Updates
21 Jul 2009 1.1

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 1.2

Contains previous update that was incorrectly uploaded

11 Jan 2011 1.4

Updated description to take into account remark by Benjamin Ting

Contact us