jsonencode

Encodes data into a JavaScript Object Notation (JSON) format string.

Syntax

R = jsonencode(data)

R = jsonencode(..., 'convertinfandnan', convert)

R = jsonencode(..., 'prettyprint', format)

Inputs

data
Any valid oml datatype of cell, Complex, logical, mtx, scalar,string or struct.
Type: cell | complex | mat | scalar | string | struct
convert
Optional argument specifying if NaN, Inf and -Inf need to be encoded as null. Valid values are true (default) and false.
Type: Boolean
format
Optional argument specifying if the JSON encoded string should have newlines and indentations. Valid values are false (default) and false.
Type: Boolean

Outputs

R
Output encoded in JSON format.
Type: string

Examples

Encode a cell in JSON format using default options:
m = [+55 -4.5 NaN -88; Inf -Inf 0.3 10e45];
s.a = 5;
s.b = 'StructVal';
data= {'foo' 2 {Inf [1:10]} s m};
R = jsonencode(data)
R = ["foo",2,[null,[1,2,3,4,5,6,7,8,9,10]],{"a":5,"b":"StructVal"},[[55,-4.5,null,-88],[null,null,0.3,1e+46]]]
Encode a matrix in JSON format without converting NaN and Inf values:
data = [+55 -4.5 NaN -88; Inf -Inf 0.3 10e45];
R = jsonencode(data, 'convertinfandnan', false)
R = [[55,-4.5,NaN,-88],[Infinity,-Infinity,0.3,1e+46]]
Encode a struct in JSON format with indentations and newlines:
data = struct('access_token','ory_at_XXShG-4C','expires_in',86399,'scope','data thing','token_type','bearer');
R = {
  "access_token":"ory_at_XXShG-4C",
  "expires_in":86399,
  "scope":"data thing",
  "token_type":"bearer"
}