Main Content


Convert MATLAB data to JSON text using MATLAB Production Server JSON schema



text = mps.json.encode(data) encodes MATLAB® data and returns JSON text in JSON schema for MATLAB Production Server™. You can use this JSON text on multiple platforms to encode content for MATLAB Production Server.


text = mps.json.encode(data,Name,Value) specifies additional options with one or more name-value pair arguments for specific input cases. For example, you can decide to encode data in the large or small format defined for representing data types.


collapse all

Encode a 3-by-3 magic square in the JSON format.

ans =

Encode a 3-by-3 magic square in JSON using the large format option.

ans =

Encode an array containing -Inf, NaN, and Inf in JSON using 'object' in 'NanInfType' option.

mps.json.encode([-Inf NaN Inf],'NaNInfType','object','Format','large')
ans =
    '{"mwdata":[{"mwdata":"-Inf"},{"mwdata":"NaN"},{"mwdata":"Inf"}], "mwsize":[1,3],"mwtype":"double"}'

Input Arguments

collapse all

MATLAB data that MATLAB Production Server supports, specified as a numeric, character, logical, structure, or cell.

Name-Value Arguments

Specify optional comma-separated pairs of Name,Value arguments. Name is the argument name and Value is the corresponding value. Name must appear inside quotes. You can specify several name and value pair arguments in any order as Name1,Value1,...,NameN,ValueN.

Example: mps.json.encode(data,'Format','large')

Format to encode MATLAB data, specified as the comma-separated pair consisting of 'Format' and the format 'small' or 'large'.

The small format is a more simple representation of MATLAB data types in JSON, whereas the large format is a more generic representation. For more information, see JSON Representation of MATLAB Data Types.

Format to encode NaN, Inf, and -Inf in data, specified as a comma-separated pair consisting of 'NaNInfType' and the JSON data-types 'string' or 'object'.

Format text for readability, specified as a comma-separated pair consisting of 'PrettyPrint' and logical 'true' or 'false'.

PrettyPrint enables better readability for a user when set to true. Syntax is mps.json.encode(magic(3),'PrettyPrint',true).

Output Arguments

collapse all

JSON-formatted text for JSON schema for MATLAB Production Server, returned as a character vector.

Introduced in R2018a