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 pairs of arguments as Name1=Value1,...,NameN=ValueN, where Name is the argument name and Value is the corresponding value. Name-value arguments must appear after other arguments, but the order of the pairs does not matter.

Before R2021a, use commas to separate each name and value, and enclose Name in quotes.

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 simpler 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.

Version History

Introduced in R2018a