2

Do you know a very fast JSON Parser for Matlab?

Currently I'm using JSONlab, but with larger JSON files (mine is 12 MB, 500 000 lines) it's really slow. Or do you have any tips' for me to increase the speed?

P.S. The JSON file is max. 3 levels deep.

4 Answers 4

3

If you want to be fast, you could use the Java JSON parser. And before this answer gets out of hand, I am going to post the stuff I put down so far:

clc

% input example
jsonStr = '{"bool1": true, "string1": "some text", "double1": 5, "array1": [1,2,3], "nested": {"val1": 1, "val2": "one"}}'

% use java..
javaaddpath('json.jar');
jsonObj = org.json.JSONObject(jsonStr);

% check out the available methods
jsonObj.methods % see also http://www.json.org/javadoc/org/json/JSONObject.html

% get some stuff
b = jsonObj.getBoolean('bool1')
s = jsonObj.getString('string1')
d = jsonObj.getDouble('double1')
i = jsonObj.getJSONObject('nested').getInt('val1')

% put some stuff
jsonObj = jsonObj.put('sum', 1+1);


% getting an array or matrix is not so easy (you get a JSONArray)
e = jsonObj.get('array1');

% what are the methods to access that JSONArray?
e.methods

for idx = 1:e.length()
    e.get(idx-1)
end

% but putting arrays or matrices works fine
jsonObj = jsonObj.put('matrix1', ones(5));

% you can get these also easily ..
m1 = jsonObj.get('matrix1')
% .. as long as you dont convert the obj back to a string
jsonObj = org.json.JSONObject(jsonObj.toString());
m2 = jsonObj.get('matrix1')
Sign up to request clarification or add additional context in comments.

1 Comment

Forgot to mention that you have to add the json.jar to the javaclasspath. Can be downloaded for instance here. Updated the answer as well.
1

If you can afford to call .NET code, you may want to have a look at this lightweight guy (I'm the author):

https://github.com/ysharplanguage/FastJsonParser#PerfDetailed

Coincidentally, my benchmark includes a test ("fathers data") in the 12MB ballpark precisely (and with a couple levels of depth also) that this parser parses into POCOs in under 250 ms on my cheap laptop.

As for the MATLAB + .NET code integration:

http://www.mathworks.com/help/matlab/using-net-libraries-in-matlab.html

'HTH,

Comments

1

If you just want to read JSON files, and have a C++11 compiler, you can use the very fast json_read mex function.

Comments

1

Since Matlab 2016b, you can use jsondecode.

I have not compared its performance to other implementations. From personal experience I can say that it is not horribly slow.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.