jsondecode

Converts JSON text into MATLAB values.

Category: Data Import and Analysis > Data Import and Export

Syntax

value = jsondecode(jsonText)

Parses the text and returns a struct, array, cell array or scalar.

Description

The mapping follows JSON structure: objects become scalar structs; arrays of numbers become column vectors; arrays of equal-length number arrays become matrices; arrays of objects with the same fields become a struct array; mixed arrays become cell arrays. true and false become logical, strings become character vectors, and null inside a numeric array becomes NaN.

Object keys that are not valid variable names are changed to valid ones, so "1bad" becomes x1bad.

Input arguments

jsonText
JSON as a character vector or string.

Output arguments

value
The decoded value; the type depends on the JSON content.

Example

Objects, arrays and null

s = jsondecode('{"name":"Ann","scores":[90,85],"ok":true}')
jsondecode('[1,2,null]')'
jsondecode('[[1,2],[3,4]]')
r = jsondecode('[{"a":1},{"a":2}]');
size(r), r(2).a
Output
s = struct with fields:

      name: 'Ann'
    scores: [2×1 double]
        ok: 1
ans = 1×3

     1     2   NaN
ans = 2×2

     1     2
     3     4
ans = 1×2

     2     1
ans = 2

See also

jsonencode fileread struct