Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

json

Methods for reading and writing JSON from files or strings.

read_file

Reads a JSON file and converts it to Lua values.

Parameters

ParameterTypeDescription
pathstringPath to JSON file.

Returns

Lua value (usually a table) representing the parsed JSON.

Example

local data = json.read_file("data.json")
fmt.print("{}", data)

read_string

Parses a JSON string into Lua values.

Parameters

ParameterTypeDescription
json_strstringJSON source string.

Returns

Lua value (usually a table).

Example

local data = json.read_string('{"name":"lush","ok":true}')

write_file

Serializes a Lua value to pretty JSON and writes it to a file.

Parameters

ParameterTypeDescription
pathstringDestination file path.
valueanyLua value to serialize.

Returns

None

Example

json.write_file("./data.json", {
  user = "aria",
  id = 1
})

write_string

Serializes a Lua value into a pretty-printed JSON string.

Parameters

ParameterTypeDescription
valueanyLua value to serialize.

Returns

JSON string.

Example

local serialized = json.write_string({ user = "aria", id = 1 })
fmt.print("{}", serialized)

Conversion Notes

  • Lua arrays map to JSON arrays only when keys are contiguous numeric indices starting at 1.
  • Lua tables with string keys map to JSON objects.
  • Unsupported Lua types (like functions/userdata) raise an error.