Skip to content

pyio.write_json

fabian-flassig edited this page Mar 18, 2026 · 3 revisions

Writes a Lua data structure to a JSON file.

pyio.write_json(path, value)
Parameter Type Description
path string The file path where the JSON should be written
value any A Lua value (typically a table) that should be serialized to JSON

Return value

Type Description
nil No return value

Example:

Writing the following Lua table

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

pyio.write_json("sample.json", data)

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.

See also:

pyio, pyio.parse_json

Clone this wiki locally