Skip to content

pyio.write_json

Daniel Flassig edited this page Jul 7, 2026 · 3 revisions

Writes a Lua data structure to a JSON file.

pyio.write_json(file, value)
Parameter Type Description
file path handle The file to write. The path handle must grant write access.
value any A Lua value (typically a table) that should be serialized to JSON

Return value

Type Description
boolean true on success, false if the file could not be written (see remarks)

Example:

Writing the following Lua table

local data = {
  Type = "TEST",
  ["My Numbers"] = {1, 2, 3.5}
}

local folder = pyui.select_folder("full")
if folder then
    pyio.write_json(folder:append_path("sample.json"), data)
end

will create a JSON file with the following content:

{
  "Type": "TEST",
  "My Numbers": [1, 2, 3.5]
}

Remarks:

Lua tables are automatically mapped to JSON structures.

If a table contains only consecutive integer keys starting at 1, it is written as a JSON array. Otherwise, it is written as a JSON object with string keys.

Lua values are converted as follows:

Lua Type JSON equivalent
table (array-like) JSON array [...]
table (key/value) JSON object {...}
string JSON string
number JSON number
boolean JSON true / false
nil JSON null

Only string keys are written for JSON objects. Non-string keys are ignored.

Unsupported Lua types (e.g. functions, userdata) are written as null.

The output encoding is UTF-8.

A writable path handle can be obtained from pyui.select_folder with "full" access. Passing a read-only handle raises an error. false is returned when the file cannot be opened or written.

Version Support:

Minimum PYTHA Version: V26

See also:

pyio, pyio.parse_json, pyui.select_folder

Clone this wiki locally