: obj = fromJSON (str)
: obj = fromJSON (str, sarray)

Convert a JSON string into a native Octave object.

fromJSON especially strives to convert numerical JSON arrays into Octave vector, matrix or ND array. Special provisions are made to recognize +/-Inf, NaN and complex numbers, which are conventionally not permited in JSON strings.

Input arguments:

  • str is a JSON string.
  • sarray, (default TRUE) logical value that determines how JSON member arrays are parsed. Setting it to FALSE is recommended for safer parsing of non-numerical, mixed-class or mixed-size arrays JSON payloads.

    Leave sarray set to TRUE for fast parsing of numerical JSON arrays and objects. Octave vectors, matrices, ND arrays and struct arrays are returned as much as possible, otherwise returned as a combination of vectors/matrices and cell arrays.

    Set sarray to FALSE if, with fast parsing, output does not match expections, particularly if str mainly comprises JSON objects/arrays with strings. All JSON member arrays are returned as (nested) Octave cell arrays.

Output:

  • obj is a native Octave object. JSON number, logical, array, and object strings are converted to Octave numbers, logicals, vectors and structs, respectively. Quoted or unrecognizable JSON fragments are returned as NaN values.

Special numbers:

The specification for JSON does not allow +/-Inf, NaN or complex numbers; nevertheless, provisions are made here to enable these important numbers:

  • JSON number strings ’+/-1e308’ are rounded to +/-Inf.
  • Unquoted JSON string ’null’ is converted to NaN.
  • JSON objects, or arrays thereof, with exclusive members "real" and "imag" (or "re" and "im") will be converted to Octave complex numbers, vectors or matrices, respectively.
    e.g. '{"re":3,"im":1}'     => 3 + 1i               
         '[1,{"re":3,"im":1}]' => [ 1 + 0i, 3 + 1i ]   
    

See also: toJSON.

Package: io