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
- Represent data or variables in the client code to serve as inputs to the MATLAB function deployed on the server.
- Parse the response from a MATLAB Production Server™ instance for further manipulation in the client code.
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.
- Small notation provides a simplified representation of MATLAB data types in JSON. There is a one-to-one mapping between MATLAB data types and their corresponding JSON representation. You can use small notation to represent scalar and multidimensional
double
andlogical
data types, scalar and 1-by-Nchar
data type and scalarstruct
. - Large notation provides a generic representation of MATLAB data types in JSON. The large format uses the JSON
object
notation consisting of property name-value pairs to represent data. You can use large notation for any MATLAB data type that cannot be represented in small notation. The response from the MATLAB Production Server uses large notation by default.
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
- The
mwdata
property must be a JSON array of JSON numbers. - The
mwtype
property can be any ofdouble
,single
,int8
,uint8
,int16
,uint16
,int32
,uint32
,int64
,uint64
. - You cannot represent scalar or multidimensional
single
and integer types using JSON small notation. - Starting in R2020a,
int64
anduint64
numbers maintain precision and range in their JSON representation as they are not converted todouble
.
Scalar Numeric Types: double
, single
and Integers
- The
mwdata
property must be a JSON array containing one JSON number representing the MATLAB scalar value. - The
mwsize
property must be a JSON array containing 1,1.
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
- The
mwdata
property must be a JSON array containing data from multidimensional arrays in column-major order. This ordering corresponds to the default memory layout in MATLAB. - You must represent
double
arrays, except N-by-1double
arrays, with nested JSON arrays when using small notation - You cannot represent multidimensional
single
and integer types using JSON small notation.
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
NaN
,Inf
, and-Inf
are numeric types whose underlying MATLAB class can be eitherdouble
orsingle
only. You cannot representNaN
,Inf
, and-Inf
as an integer type in MATLAB.
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
- The
mwdata
property values must contain the real and imaginary parts of the complex number represented side by side. - You must set an additional property
mwcomplex
with the value oftrue
. - The
mwtype
property can be any ofdouble
,single
,int8
,uint8
,int16
,uint16
,int32
,uint32
,int64
,uint64
. - You cannot represent complex numbers using small notation.
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
- The
mwdata
property must be an array of JSONstrings
. - The
mwtype
property must have the value ofchar
. - You can represent scalar characters and 1-by-N character arrays in small notation.
- You cannot represent multidimensional character arrays in large notation.
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
- The
mwdata
property must contain only JSONtrue
orfalse
boolean values. For multidimensionallogical
data, represent the values in column-major order. - The
mwtype
property must have the value oflogical
. - In the small notation, you must represent multidimensional
logical
arrays with nested JSON arrays.
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
- The
mwdata
property must be a JSONarray
that contains the values of the cells in their JSON representation. - The
mwtype
property must have the value ofcell
. - You cannot represent
cell
arrays using small notation.MATLAB Data Type JSON Large Notation cell { "mwdata": [], "mwsize": [], "mwtype": "cell" } - Although you must represent cell arrays using large notation only, if the data type of a
cell
element supports small notation, you can represent that element in small notation when encoding thecell
array in JSON.
The following table shows an example.MATLAB Data: cell Array JSON Large Notation with somecell elements in Small Notation JSON Large Notation with all cell elements in Large Notation {'Primes',[10 23 199],{false,true,'maybe'}} { "mwdata": ["Primes", [[10, 23, 199]], { "mwdata": [false, true, "maybe"], "mwsize": [1, 3], "mwtype": "cell" } ], "mwsize": [1, 3], "mwtype": "cell" } { "mwdata": [{ "mwdata": ["Primes"], "mwsize": [1, 6], "mwtype": "char" },{ "mwdata": [10, 23, 199], "mwsize": [1, 3], "mwtype": "double" },{ "mwdata": [{ "mwdata": [false], "mwsize": [1, 1], "mwtype": "logical" },{ "mwdata": [true], "mwsize": [1, 1], "mwtype": "logical" },{ "mwdata": ["maybe"], "mwsize": [1, 5], "mwtype": "char" }], "mwsize": [1, 3], "mwtype": "cell" }], "mwsize": [1, 3], "mwtype": "cell" } - For more information on MATLAB
cell
data type, see cell (MATLAB).
Structure Array
- The
mwdata
property must be a JSON object that contains name-value pairs, where the name matches a field in thestruct
and value is a JSON array that represents the data in the field. - The
mwtype
property must have the value ofstruct
. - Although you must represent multidimensional
struct
arrays using JSON large notation, if the data type of astruct
value supports small notation, you can represent that value in small notation when encoding thestruct
in JSON. - You can represent only a scalar
struct
in small notation.
JSON Representation ofstruct
arraysMATLAB Data Type JSON Small Notation (valid only for a scalar struct) JSON Large Notation struct JSON object { "mwdata": {} "mwsize": [], "mwtype": "struct" } MATLAB Data: Scalar Structure Array JSON Small Notation JSON Large Notation struct('name','John Smith','age',15) { "name" : "John Smith", "age" : 15 } { "mwdata": { "age": [{ "mwdata": [15], "mwsize": [1, 1], "mwtype": "double" }], "name": [{ "mwdata": ["John Smith"], "mwsize": [1, 10], "mwtype": "char" }] }, "mwsize": [1, 1], "mwtype": "struct" } - Although you can represent 1-by-1
struct
arrays in small notation, if the data type of astruct
value does not support small notation, you must represent that value in large notation when encoding thestruct
in JSON.MATLAB Data: 1-by-1 Structure Array JSON Small Notation with somestruct values in Large Notation JSON Large Notation with allstruct values in Large Notation struct('Name',{{'Casper','Ghost'}},... 'Age',{[14,17,18]},... 'Date', {736676}) { "Age": [[14, 17, 18]], "Date": 736676, "Name": { "mwdata": ["Casper", "Ghost"], "mwsize": [1, 2], "mwtype": "cell" } } { "mwdata": { "Age": [{ "mwdata": [14, 17, 18], "mwsize": [1, 3], "mwtype": "double" }], "Date": [{ "mwdata": [736676], "mwsize": [1, 1], "mwtype": "double" }], "Name": [{ "mwdata": [{ "mwdata": ["Casper"], "mwsize": [1, 6], "mwtype": "char" }, { "mwdata": ["Ghost"], "mwsize": [1, 5], "mwtype": "char" }], "mwsize": [1, 2], "mwtype": "cell" }] }, "mwsize": [1, 1], "mwtype": "struct" } - Although you must represent multidimensional
struct
arrays using JSON large notation, if the data type of astruct
value supports small notation, you can represent that value in small notation when encoding thestruct
in JSON.MATLAB Data: Multidimensional Structure Array JSON Large Notation with somestruct values in Small Notation JSON Large Notation with allstruct values in Large Notation struct( 'Name',{'Casper','Ghost';... 'Genie' ,'Wolf'},... 'Ages',{14,17;... 20,23} ) { "mwdata":{ "Ages":[14,20,17,23], "Name":["Casper","Genie","Ghost","Wolf"] }, "mwsize":[2,2], "mwtype":"struct" } { "mwdata":{ "Ages":[{ "mwdata":[14], "mwsize":[1,1], "mwtype":"double" },{ "mwdata":[20], "mwsize":[1,1], "mwtype":"double" },{ "mwdata":[17], "mwsize":[1,1], "mwtype":"double" },{ "mwdata":[23], "mwsize":[1,1], "mwtype":"double" }], "Name":[{ "mwdata":["Casper"], "mwsize":[1,6], "mwtype":"char" },{ "mwdata":["Genie"], "mwsize":[1,5], "mwtype":"char" },{ "mwdata":["Ghost"], "mwsize":[1,5], "mwtype":"char" },{ "mwdata":["Wolf"], "mwsize":[1,4], "mwtype":"char" }] }, "mwsize":[2,2], "mwtype":"struct" } - For more information on MATLAB
struct
data type, see struct (MATLAB).
String Array
- The
mwdata
property must be a JSON array containing strings in column-major order. - The
mwtype
property must have the value ofstring
. - You cannot represent
string
arrays using small JSON notation.
JSON Representation ofstring
arraysMATLAB Data Type JSON Large Notation string { "mwdata": [JSON string], "mwsize": [], "mwtype": "string" } MATLAB Data: Scalar, Single-dimensional, Multidimensional, and missing (MATLAB) string Arrays JSON Large Notation ------------------------------------------------------------------------------------------------------------------------------ ----------------------------------------------------------------------------------------------- "abc" { "mwdata": ["abc"], "mwsize": [1, 1], "mwtype": "string" } ["abc"] { "mwdata": ["abc"], "mwsize": [1, 1], "mwtype": "string" } ["abc" "de"] { "mwdata": ["abc", "de"], "mwsize": [1, 2], "mwtype": "string" } ["abc" "de"; "fg" "hi"] { "mwdata": ["abc", "fg", "de", "hi"], "mwsize": [2, 2], "mwtype": "string" } string(missing) { "mwdata": [{"mwdata": "missing"}], "mwsize": [1, 1], "mwtype": "string" } - For more information on MATLAB
string
data type, see string, " " (MATLAB).
Enumeration
- The
mwdata
property must be a JSON array of strings denoting the enumeration members. - The
mwtype
property must be set to the class of the enumerations in the array. - You cannot represent an
enumeration
using small JSON notation.
JSON Representation ofenumeration
MATLAB Data Type JSON Large Notation enumeration { "mwdata": [JSON string], "mwsize": [], "mwtype": "" } The following table shows examples of JSON representation of an enumeration
.Use the following enumeration for the examples. For more information, seeDefine Enumeration Classes (MATLAB). classdef Colors enumeration
endBlack Blue Red
endMATLAB Data: Object of Enumeration Class JSON Large Notation b = Colors.Black { "mwdata": ["Black"], "mwsize": [1, 1], "mwtype": "Colors" } b = [Colors.Black Colors.Blue] { "mwdata": ["Black", "Blue"], "mwsize": [1, 2], "mwtype": "Colors" } - For more information on MATLAB
enumeration
data type, see enumeration (MATLAB).
Datetime Array
- The
mwdata
property must be a JSON object containing name-value pairs forTimeStamp
and optionally forLowOrderTimeStamp
. Values forTimeStamp
andLowOrderTimeStamp
are JSON representation of thedouble
data type.- The
TimeStamp
property values represent the POSIX time in milliseconds elapsed since 00:00:00 1-Jan-1970 UTC (Coordinated Universal Time). - The
LowOrderTimeStamp
property values represent additional resolution in the timestamp. Use this property to maintain precision past milliseconds. - Although you must represent
datetime
arrays using large notation only, sinceTimeStamp
andLowOrderTimeStamp
represent values of thedouble
data type which supports small notation, you can representTimeStamp
andLowOrderTimeStamp
using small notation when encodingdatetime
arrays in JSON.
- The
- The
mwmetadata
property must be a JSON object containing name-value pairs forTimeZone
andFormat
. Values forTimeZone
andFormat
are JSON representation of thechar
data type.- The values of the
TimeZone
andFormat
properties contain metadata necessary for recreating thedatetime
values with timezones in MATLAB in their original display format. This metadata is necessary because the numeric values contained in theTimeStamp
andLowOrderTimeStamp
arrays are calculated with respect to UTC. - You can specify
TimeZone
andFormat
properties forNaT
andInf
datetime
array values. - Although you must represent
datetime
arrays using large notation only, sinceTimeZone
andFormat
represent values of thechar
data type which supports small notation, you can representTimeZone
andFormat
using small notation when encodingdatetime
arrays in JSON. - The value for
TimeZone
can be empty. - The default value for
Format
depends on your system locale. For more information, see Default datetime Format (MATLAB).
- The values of the
- The
mwtype
property must have the value ofdatetime
. - You cannot represent
datetime
arrays using small JSON notation.
JSON Representation ofdatetime
arraysMATLAB Data Type JSON Large Notation datetime { "mwdata": { "LowOrderTimeStamp": "TimeStamp": }, "mwmetadata": { "TimeZone": , "Format": }, "mwsize": [], "mwtype": "datetime" } MATLAB Data: Scalar datetime Array JSON Large Notation withmwdata andmwmetadata in Small Notation ---------------------------------- ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- datetime(2015, 3, 24); { "mwdata": { "TimeStamp": 1.4271552E+12 }, "mwmetadata": { "Format": "dd-MMM-uuuu", "TimeZone": "" }, "mwsize": [1, 1], "mwtype": "datetime" } The following table shows JSON representation for a datetime
row vector. SinceLowOrderTimeStamp
andTimeStamp
containdouble
values, you need to use nested JSON arrays when representing multidimensional (except N-by-1) arrays ofLowOrderTimeStamp
andTimeStamp
in small notation.MATLAB Data: datetime Row Vector JSON Large Notation withmwdata andmwmetadata in Small Notation -------------------------------------------------------------------------- ----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- datetime(2018,1,8,10,... 11,12,(1:5)+(1:5)*1e-6,... 'TimeZone','local'); { "mwdata": { "LowOrderTimeStamp": [[9.9999999991773336E-7, 1.9999999998354667E-6, 2.9999999999752447E-6, 3.9999999996709334E-6, 4.9999999998107114E-6]], "TimeStamp": [[1.515424272001E+12, 1.515424272002E+12, 1.515424272003E+12, 1.515424272004E+12, 1.515424272005E+12]], }, "mwmetadata": { "Format":"dd-MMM-uuuu HH:mm:ss", "TimeZone": "America\/New_York", }, "mwsize": [1, 5], "mwtype": "datetime" } MATLAB Data: datetime Column Vector JSON Large Notation withmwdata andmwmetadata in Small Notation --------------------------------------------------------------------------- --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- datetime(2018,1,8,10,... 11,12,(1:5)+(1:5)*1e-6,... 'TimeZone','local')'; { "mwdata": { "LowOrderTimeStamp": [9.9999999991773336E-7, 1.9999999998354667E-6, 2.9999999999752447E-6, 3.9999999996709334E-6, 4.9999999998107114E-6], "TimeStamp": [1.515424272001E+12, 1.515424272002E+12, 1.515424272003E+12, 1.515424272004E+12, 1.515424272005E+12], }, "mwmetadata": { "Format":"dd-MMM-uuuu HH:mm:ss", "TimeZone": "America\/New_York", }, "mwsize": [5, 1], "mwtype": "datetime" } MATLAB Data: NaT andInf datetime Array JSON Large Notation withmwdata andmwmetadata in Small Notation -------------------------------------- ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- NaT { "mwdata": { "TimeStamp": { "mwdata": "NaN" } }, "mwmetadata": { "Format": "dd-MMM-uuuu HH:mm:ss", "TimeZone": "" }, "mwsize": [1, 1], "mwtype": "datetime" } datetime(inf,inf,inf) { "mwdata": { "TimeStamp": { "mwdata": "Inf" } }, "mwmetadata": { "Format": "dd-MMM-uuuu HH:mm:ss", "TimeZone": "" }, "mwsize": [1, 1], "mwtype": "datetime" } - For more information on MATLAB
datetime
data type, see datetime (MATLAB).
Empty Array: []
- Empty arrays
[]
cannot be of typestruct
.
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" "" } |