Matlab JSON

This package contains Matlab functions to serialize/decode matlab object in JSON format. The software internally uses org.json package to convert json to java object and then translates it into Matlab object.


json_v0.3.tgz json_v0.2.tgz

Or go to Github for the latest version.


All functions are scoped under json namespace.

dumpEncode matlab value into a JSON string.
loadLoad matlab value from a JSON string.
readLoad a matlab value from a JSON file.
writeWrite a matlab value into a JSON file.


Add path to the directory containing +json before use.

To serialize matlab object:

  >> X = struct('field1', magic(2), 'field2', 'hello');
  >> S = json.dump(X);
  >> disp(S);

To decode json string:

  >> X = json.load(S);
  >> disp(X);
      field2: 'hello'
      field1: [2x2 double]

There are also convenient functions and json.write(filepath,X) to directly read from or write to a file.

  >> json.write('/path/to/your.json', X);
  >> X ='/path/to/your.json');


Due to the multiple ways to represent an array in Matlab (i.e., numeric array, cell array, or struct array), it is impossible to represent everything in a compatible format. For example, a json string "[[1,2],[3,4]]" can be interpreted in different ways in Matlab, such as [1,2;3,4], {1,2;3,4}, {[1,2],[3,4]}, etc. Because of this, json.load does not always yield the exactly same input to json.dump.

This implementation is designed to maximize the ease of data exchange. For that purpose, by default, json parser assumes the following.

For example, a nested array with the same sized elements is treated as an N-D array.

  >> x = json.load('[[[1,2],[3,4]],[[5,6],[7,8]]]')

  x(:,:,1) =
       1     2
       3     4

  x(:,:,2) =
       5     6
       7     8

The json.load function can take an option to specify column-major interpretation or cell-array precedence. Check help json.load for details.


To run a test, use test_all function in the test directory.

  >> addpath('test');
  >> test_all;


You may redistribute this software under BSD license.



JSON in Java: