Previous: Numerical Data and Strings, Up: Converting Strings [Contents][Index]
JavaScript Object Notation, in short JSON, is a very common human readable and structured data format. GNU Octave supports decoding and encoding this format with the following two functions.
Decode text that is formatted in JSON.
The input json is a string that contains JSON text. The output object is an Octave object that contains the result of decoding json.
For more information about the options "ReplacementStyle"
and
"Prefix"
, see
matlab.lang.makeValidName.
-NOTE: It is not guaranteed to get the same JSON text if you decode
and then encode it as some names may change by
matlab.lang.makeValidName
.
This table shows the conversions from JSON data types to Octave data types:
"Boolean"
Scalar "logical"
"Number"
Scalar "double"
"String"
"Vector"
of chars
"Object"
Scalar "struct"
(field names of the struct may be different from
the keys of the JSON object due to
matlab.lang.makeValidName)
"Array"
of different data types"Cell"
array
"Array"
of booleans"Array"
of logicals
"Array"
of numbers"Array"
of doubles
"Array"
of strings"Cell"
array of vectors of chars
"Array"
of JSON objects (All objects have the same field names)"Struct array"
"Array"
of JSON objects (Objects have different field names)"Cell"
array of scalar structs
"null"
inside a numeric array"NaN"
"null"
inside a non-numeric arrayEmpty "Array"
of doubles ("[]"
)
Examples:
jsondecode ('[1, 2, null, 3]') ⇒ ans = 1 2 NaN 3
jsondecode ('["foo", "bar", ["foo", "bar"]]') ⇒ ans = { [1,1] = foo [2,1] = bar [3,1] = { [1,1] = foo [2,1] = bar } }
jsondecode ('{"nu#m#ber": 7, "s#tr#ing": "hi"}', 'ReplacementStyle', 'delete') ⇒ scalar structure containing the fields: number = 7 string = hi
jsondecode ('{"1": "one", "2": "two"}', 'Prefix', 'm_') ⇒ scalar structure containing the fields: m_1 = one m_2 = two
See also: jsonencode, matlab.lang.makeValidName.
Encode Octave’s data types into JSON text.
The input object is the Octave’s object we want to encode. The output json is the JSON text that contains the result of encoding object.
If the value of the option "ConvertInfAndNaN"
is true, "NaN"
,
"Inf"
and "-Inf"
values will be converted to "null"
value in the output. If it is false, they will remain with their
original values. The default value for this option is true.
If the value of the option "PrettyWriter"
is true, the output text will
have indentations and line feeds. If it is false, the output will be condensed
and without any white-spaces. The default value for this option is false.
-NOTES:
"classdef"
objects and "containers.Map"
objects are converted
into structs then encoded as structs.
"int32"
then decoded it, you will get a "double"
.
This table shows the conversions from Octave data types to JSON data types:
"logical"
"Boolean"
"NaN"
, "Inf"
, "-Inf"
"null"
"Number"
"Numeric vector"
"Numeric array"
"Numeric array"
Nested "numeric array"
"Logical vector"
"Boolean array"
"Logical array"
Nested "boolean array"
"Char vector"
"String"
"Char array"
Nested "string array"
"cell"
"array"
with a single element
"Cell vector"
"Array"
"Cell array"
single dimensional "array"
"struct"
"JSON Object"
"Struct vector"
"JSON objects array"
"Struct array"
Nested "JSON objects array"
"classdef"
objects"JSON object"
"containers.Map"
"JSON object"
Examples:
jsonencode ([1 NaN; 3 4]) ⇒ [[1,null],[3,4]]
jsonencode ([1 NaN; 3 4], "ConvertInfAndNaN", false) ⇒ [[1,NaN],[3,4]]
jsonencode ([true; false], "ConvertInfAndNaN", false, "PrettyWriter", true) ⇒ ans = [ true, false ]
jsonencode (['foo', 'bar'; 'foo', 'bar']) ⇒ ["foobar","foobar"]
jsonencode (struct ('a', Inf, 'b', [], 'c', struct ())) ⇒ {"a":null,"b":[],"c":{}}
jsonencode (struct ('structarray', struct ('a', {1; 3}, 'b', {2; 4}))) ⇒ {"structarray":[{"a":1,"b":2},{"a":3,"b":4}]}
jsonencode ({'foo'; 'bar'; {'foo'; 'bar'}}) ⇒ ["foo","bar",["foo","bar"]]
jsonencode (containers.Map({'foo'; 'bar'; 'baz'}, [1, 2, 3])) ⇒ {"bar":2,"baz":3,"foo":1}
See also: jsondecode.
Previous: Numerical Data and Strings, Up: Converting Strings [Contents][Index]