JSON Representation of MATLAB Data Types - MATLAB & Simulink (original) (raw)

JavaScript Object Notation or JSON is a text-based data interchange format that can be used across programming languages. Since JSON is independent of programming language, you can represent MATLAB® data types in JSON. Using the JSON representation of MATLAB data types, you can

The response from the server contains a JSON array, where each element of the array corresponds to an output of the deployed MATLAB function represented as a JSON object.

You can represent MATLAB data types in JSON using two notation formats: small and large.

A JSON object in the large notation contains the following property name-value pairs.

Property Name Property Value
"mwdata" JSON array representing the actual data. Specify the property value by enclosing the data as a comma-separated list within [].
"mwsize" JSON array representing the dimensions of the data. Specify the property value by enclosing the dimensions as a comma-separated list within [].
"mwtype" JSON string representing the type of data. Specify the property value within"". "double" | "single" "int8" "uint8" "int16" "uint16" "int32" "uint32" "int64" "uint64" "logical" "char" "struct" "cell" "string" "datetime"
"mwcomplex" For complex numbers, set the property value to JSONtrue.

MATLAB Compiler SDK™ provides the following utility functions for data conversion between MATLAB and JSON.

Function Name Purpose
mps.json.encoderequest (MATLAB Compiler SDK) Convert MATLAB data in a server request to JSON text using MATLAB Production Server JSON schema.
mps.json.decoderesponse (MATLAB Compiler SDK) Convert JSON text from a server response to MATLAB data.
mps.json.encode (MATLAB Compiler SDK) Convert MATLAB data to JSON text using MATLAB Production Server JSON schema.
mps.json.decode (MATLAB Compiler SDK) Convert a character vector or string in MATLAB Production Server JSON schema to MATLAB data.

The RESTful API supports the following MATLAB data types.

Numeric Types: double, single and Integers

Scalar Numeric Types: double, single and Integers

JSON Representation of Scalar Numeric Types: double,single and Integers

MATLAB Data Type JSON Small Notation JSON Large Notation
single, int8,uint8, int16,uint16,int32,uint32, int64,uint64 No small representation { "mwdata": [JSON number], "mwsize": [1,1], "mwtype": "single" | "int8" "uint8" "int16" "uint16" "int32" "uint32" "int64" "uint64" }
double JSON number { "mwdata": [JSON number], "mwsize": [1,1], "mwtype": "double" }
MATLAB Data: Scalar Numerics JSON Small Notation JSON Large Notation
int8(23) No small representation { "mwdata": [23], "mwsize": [1,1], "mwtype": "int8" }
uint8(27) No small representation { "mwdata": [27], "mwsize": [1,1], "mwtype": "uint8" }
single(20.15) No small representation { "mwdata": [20.15], "mwsize": [1,1], "mwtype": "single" }
intmax('int64') No small representation { "mwdata": [9223372036854775807], "mwsize": [1,1], "mwtype": "int64" }
double(12.905) 12.905 { "mwdata": [12.905], "mwsize": [1,1], "mwtype": "double" }
42 42 { "mwdata": [42], "mwsize": [1,1], "mwtype": "double" }

Multidimensional Numeric Types: double, single and Integers

MATLAB Data: Multidimensionaldouble Array JSON Small Notation JSON Large Notation
[1,2,3;... 4,5,6] [[1,2,3],[4,5,6]] { "mwdata":[1,4,2,5,3,6], "mwsize":[2,3], "mwtype":"double" }
[1, NaN, -Inf;... 2, 105, Inf] [[1,{"mwdata": "NaN"},{"mwdata": "-Inf"}],[2,105,{"mwdata": "Inf"}]] { "mwdata":[1, 2, "NaN", 105, "-Inf", "Inf"], "mwsize":[2,3], "mwtype":"double" }
[ 1 2; 4 5; 7 8 ] [[1, 2], [4, 5], [7, 8]] { "mwdata":[1,4,7,2,5,8], "mwsize":[3,2], "mwtype":"double" }
a(:,:,1) = 1 2 3 4 5 6 a(:,:,2) = 7 8 9 10 11 12 [[[1,7],[2,8]],[[3,9],[4,10]],[[5,11],[6,12]]] { "mwdata":[1,3,5,2,4,6,7,9,11,8,10,12], "mwsize":[3,2,2], "mwtype":"double" }
[17;500] [17,500] { "mwdata":[17,500], "mwsize":[2,1], "mwtype":"double" }
[17,500] [[17,500]] { "mwdata":[17,500], "mwsize":[1,2], "mwtype":"double" }

Numeric Types: NaN, Inf, and -Inf

MATLAB Data: NaN, Inf, and -Inf JSON Small Notation JSON Large Notation
NaN {"mwdata":"NaN"} { "mwdata": ["NaN"], "mwsize": [1,1], "mwtype": "double" } or{ "mwdata": [{"mwdata":"NaN"}], "mwsize": [1,1], "mwtype": "double" }
Inf {"mwdata":"Inf"} { "mwdata": ["Inf"], "mwsize": [1,1], "mwtype": "double" } or{ "mwdata": [{"mwdata":"Inf"}], "mwsize": [1,1], "mwtype": "double" }
-Inf {"mwdata":"-Inf"} { "mwdata": ["-Inf"], "mwsize": [1,1], "mwtype": "double" } or{ "mwdata": [{"mwdata":"-Inf"}], "mwsize": [1,1], "mwtype": "double" }

Numeric Types: Complex Numbers

JSON Representation of Complex Numbers

MATLAB Data JSON Large Notation
a + bi { "mwcomplex": true, "mwdata": [a,b], "mwsize": [1,1], "mwtype": "double" }
MATLAB Data: Scalar Complex Number JSON Large Notation
int32(3 + 4i) { "mwcomplex": true, "mwdata": [3,4], "mwsize": [1,1], "mwtype": "int32" }
MATLAB Data: Multidimensional Array of Complex Numbers JSON Large Notation
[1 - 2i;... 3 + 7i] { "mwcomplex": true, "mwdata":[1, -2, 3, 7], "mwsize":[2,1], "mwtype":"double", }

Character Array

JSON Representation of char

MATLAB Data Type JSON Small Notation (for scalar and single dimensional character arrays) JSON Large Notation
char JSON string { "mwdata": [JSON string], "mwsize": [], "mwtype": "char" }
MATLAB Data: Scalar and Single-dimensional Character Array JSON Small Notation JSON Large Notation
'a' "a" { "mwdata": ["a"], "mwsize": [1,1], "mwtype": "char" }
'hey, jude' "hey, jude" { "mwdata": ["hey, jude"], "mwsize": [1,9], "mwtype": "char" }
MATLAB Data: Multidimensional Character Array JSON Large Notation
['boston';... '123456'] { "mwdata": ["b1o2s3t4o5n6"], "mwsize": [2,6], "mwtype": "char" }

Logical

JSON Representation of logical

MATLAB Data Type JSON Small Notation JSON Large Notation
logical true | false { "mwtype": "logical", "mwsize": [1,1], "mwdata": [true | false] }
MATLAB Data: Scalar logical JSON Small Notation JSON Large Notation
logical(1) ortrue true { "mwdata": [true], "mwsize": [1,1], "mwtype": "logical" }
logical(0) orfalse false { "mwdata": [false], "mwsize": [1,1], "mwtype": "logical" }
MATLAB Data: Multidimensional logical Array JSON Small Notation JSON Large Notation
[true,false;... true,false;... true,false] [[[true,false],[true,false],[true,false]]] { "mwdata": [true,true,true,false,false,false], "mwsize": [3,2], "mwtype": "logical" }

Cell Array

Structure Array

String Array

Enumeration

Datetime Array

Empty Array: []

MATLAB Data: Empty Array JSON Small Notation JSON Large Notation
[] [] { "mwdata": [], "mwsize": [0,0], "mwtype": "double" | "single" "int8" "uint8" "int16" "uint16" "int32" "uint32" "int64" "uint64" "logical" "char" "cell" "string" "" }

See Also

Topics

External Websites