use json in matlab in matlab

Matlab has built-in support for JSON (JavaScript Object Notation) through the functions jsonencode and jsondecode.

To parse a JSON string, use jsondecode. It takes a string and returns a MATLAB structure.

main.m
json_string = '{"name":"John", "age":30, "city":"New York"}';
data = jsondecode(json_string)
93 chars
3 lines

To create a JSON string from a MATLAB structure, use jsonencode. It takes a MATLAB structure and returns a JSON string.

main.m
data.name = 'John';
data.age = 30;
data.city = 'New York';
json_string = jsonencode(data)
90 chars
5 lines

In both cases, the output variables can be further parsed or used as necessary.

related categories

gistlibby LogSnag