jsondecode

Decodes a JavaScript Object Notation (JSON) string to an OML object.

Syntax

R = jsondecode(data)

R = jsondecode(data, property, value ....)

Inputs

data
A JavaScript Object Notation (JSON) encoded string.
Type: string
property, value
makevalidname
Optional argument specifying if field names in any struct in the output R will be checked for validity. Valid values are true (default), where prefixstring and style are applied or false.
Type: Boolean
prefix
Optional string used to prefix struct field names, if they contain only numeric or any other non-alpha numeric characters apart from underscores: '_'. The default prefixstring is 'x'. prefixstring is applied only if checkname is true.
Type: string
replacementstyle
Optional string used to specify the style how any characters that are not alphanumeric or underscores are handled. style is applied only if checkname is true. Valid values are:
  • underscore(invalid characters are replaced by underscores, default)
  • delete(invalid characters are deleted)
  • hex(invalid characters are displayed in hex notation)
Type: string

Outputs

R
OML object decoded from data.
Type: cell | mat | scalar | string | struct

Examples

Decode a JSON format string to a cell:
cell1 = {Inf 'foo' 2+ 3i 53 {} [1:10]};
encode = jsonencode(cell1)
decode = jsondecode(encode)
encode = [null,"foo",[2],53,[],[1,2,3,4,5,6,7,8,9,10]]
decode =
{
  [1,1] [Matrix] 0 x 0
  [2,1] foo
  [3,1] 2
  [4,1] 53
  [5,1] [Matrix] 0 x 0
  [6,1] [Matrix] 1 x 10
  1  2  3  4  5  6  7  8  9  10
}
Decode a JSON format string to a struct array:
s.a='val1';
s.b = eye(2,3);
s(1,2).a='val2';
s(2,2).b = 53 + i;

encode1 = jsonencode(s)
decode1 = jsondecode(encode1)
decode1(4, 1).b
encode1 = [{"a":"val1","b":[[1,0,0],[0,1,0]]},{"a":[],"b":[]},{"a":"val2","b":[]},{"a":[],"b":[53]}]
decode1 = struct [
  Struct array of size 4 x 1 with fields:
  a
  b
]
ans = 53
Decode a JSON format string to a struct array with a prefix:
R = jsondecode('{"1":"one", "2":"two"}', 'prefix', 'field_')
R = struct [
  field_1: one
  field_2: two
]
Decode a JSON format string to a struct array, replacing invalid characters with their hex notation :
R = jsondecode('{"first$one":"one", "2/":"two"}', 'replacementstyle', 'hex')
R = struct [
  first0x24one: one
  x20x2F: two
]