File Exchange

image thumbnail

JSON encode/decode

version (10.1 KB) by Léa Strobino
Parses a JSON string and returns a MATLAB object or encodes a MATLAB object into a JSON string


Updated 02 Jun 2018

GitHub view license on GitHub

json_decode parses a JSON string and returns a MATLAB object. JSON objects are converted to structures and JSON arrays are converted to vectors (all elements of the same type) or cell arrays (different types). 'null' values are converted to NaN.
json_encode encodes a MATLAB object into a JSON string. Structures are converted to JSON objects and arrays are converted to JSON arrays. Inf values are converted to the string "Inf". NaN values are converted to 'null'.

This function implements a superset of JSON as specified in the original RFC 4627 - it will also decode scalar types and NULL. RFC 4627 only supports these values when they are nested inside an array or an object. Although this superset is consistent with the expanded definition of "JSON text" in the newer RFC 7159 (which aims to supersede RFC 4627), this may cause interoperability issues with older JSON parsers that adhere strictly to RFC 4627 when encoding a single scalar value. See for more information.

mex json_decode.c jsmn.c
mex json_encode.c


url = ',46.23,6.12,46.24';
metar = json_decode(urlread(url));

s = struct(); = 'John Doe';
s.patient.billing = 127.00;
s.patient.test = [79, 75, 73; 180, 178, 177.5; 220, 210, 205];
s.patient(2).name = 'Ann Lane';
s.patient(2).billing = 28.50;
s.patient(2).test = [68, 70, 68; 118, 118, 119; 172, 170, 169];
s.patient(3).name = 'New Name';

Cite As

Léa Strobino (2020). JSON encode/decode (, GitHub. Retrieved .

Comments and Ratings (4)

@Matias: I am using the same SDK, and you can see here --


When attempting to encode a large structure array, it returns a 1x0 empty char array. Maybe a memory allocation problem...?

Hi Matias,

In order to cross-platform code, I've always used gcc to compile all mex files, so I think the problem comes from the compiler.
I only tested gcc under macOS (through Xcode) and MinGW-w64 under Windows (now available as an add-on, but it also worked fine using gnumex).

By the way, this function is becoming obsolete since MATLAB R2016b includes jsondecode built-in, which is roughly the same except that JSON arrays containing only one type are converted into MATLAB arrays instead of cell arrays as in mine. I will try to update the VLC class to use jsondecode if it's available.

Hi Lea,

I've downloaded these parsers as I wish to use your VLC class. Unfortunately I can only compile the encoder. I try to compile it the same way as you have shown above ''mex json_decode.c jsmn.c", but it gives numerous errors regarding syntax.

I'm trying to build the files with 'Microsoft Windows SDK 7.1 (C)', which works on the encoder.

Any ideas to what the problem might be?


Updated documentation

Fix null values & arrays of the same type

MATLAB Release Compatibility
Created with R2012b
Compatible with any release
Platform Compatibility
Windows macOS Linux