Scilab Website | Contribute with GitLab | Mailing list archives | ATOMS toolboxes
Scilab Online Help
2025.1.0 - 日本語


toJSON

Convert a Scilab variable to a JSON string.

Syntax

result = toJSON(var)
result = toJSON(var, indent)

toJSON(var, filename)
toJSON(var, filename, indent)
toJSON(var, indent, filename)

Arguments

var

The Scilab variable to convert to JSON format.

filename

File where to write the JSON string.

indent

If this argument is given, the result is indented. The value is the number of spaces. 0 : No identation. (default) >0 : indentation with spaces.

<opt_args>

This represents a sequence of statements key1=value1, key2=value2, ... where key1, key2, ... can be one of the following:

convertInfAndNaN

JSON specification does not allow writing NaN and Infinity values. By default (convertInfAndNaN=%t) those values are converted into a JSON null value to follow the specification. When this optional argument is set to %f, those values are stored in the JSON as literals: NaN, Infinity, and -Infinity.

result

result is a string that contains the data converted to JSON. If a filename is given, no result is returned. A Scilab scalar variable is converted into a single number without brackets. To force the brackets around, put the scalar into a list().

Description

This function converts a Scilab variable into a JSON string or JSON file.

Examples

// structure
st = struct("status", "OK", "value", 12);
toJSON(st)
// matrix
toJSON([1, 2, 3])
// pretty print
st = struct("test", ["a" "b"], "values", [1 2]);
toJSON(st)
toJSON(st, 4)
// Scalar value
toJSON(42)
toJSON(list(42))
// non finite values
toJSON([[%nan, 1]; [1e+380, 1e-380]; [%inf, -%inf]])
toJSON([[%nan, 1]; [1e+380, 1e-380]; [%inf, -%inf]], convertInfAndNaN=%f)

See also

History

バージョン記述
6.1 Function introduced.
2023.0.0 Handling of tabs (ascii(9)) changed.
2024.1.0 convertInfAndNaN option added.
Report an issue
<< http_upload Web Tools url_decode >>

Copyright (c) 2022-2024 (Dassault Systèmes)
Copyright (c) 2017-2022 (ESI Group)
Copyright (c) 2011-2017 (Scilab Enterprises)
Copyright (c) 1989-2012 (INRIA)
Copyright (c) 1989-2007 (ENPC)
with contributors
Last updated:
Thu May 22 12:57:17 CEST 2025