jsonencode

Converts MATLAB values into JSON text.

Category: Data Import and Analysis > Data Import and Export

Syntax

jsonText = jsonencode(value)

Returns the JSON text for a struct, array, cell array, string or scalar.

jsonText = jsonencode(value, 'PrettyPrint', true)

Adds line breaks and indentation.

Description

Scalars become numbers, vectors become arrays, matrices become arrays of row arrays, and structs become objects. A struct array becomes an array of objects, and a cell array becomes an array of its elements. Logical values become true or false, NaN becomes null, and an empty array becomes []. Numbers are written with 15 significant digits.

Input arguments

value
Value to encode.
'PrettyPrint'
Logical flag for readable, indented output.

Output arguments

jsonText
Character vector holding the JSON.

Example

Encode structs and arrays

jsonencode(struct('a', 1, 'b', [1 2 3]))
jsonencode(struct('n', [1 2; 3 4], 's', 'x', 'e', []))
jsonencode({1, 'a', true})
jsonencode(struct('a', {1, 2}))
jsonencode(pi)
Output
ans = '{"a":1,"b":[1,2,3]}'
ans = '{"n":[[1,2],[3,4]],"s":"x","e":[]}'
ans = '[1,"a",true]'
ans = '[{"a":1},{"a":2}]'
ans = '3.14159265358979'

See also

jsondecode struct fprintf disp