Turn Lua tables into JSON text and back.
json.encode#since 1.0
json.encode(value: any, pretty?: boolean): string
| Parameter | Type | Description |
|---|
value | any | A table, string, number or boolean. Functions and cycles are errors. |
pretty optional | boolean | Indent the output. default false |
| Returns | Description |
|---|
string | The JSON text. |
lua
local text = json.encode({ name = "x", list = { 1, 2, 3 } }, true)
json.decode#since 1.0
json.decode(text: string): any | nil, string?
| Parameter | Type | Description |
|---|
text | string | JSON text. |
| Returns | Description |
|---|
any | nil | The value, or nil when the text is not valid JSON. |
string? | The error message when it is nil. |
lua
local data, err = json.decode(response.body)
if not data then log.warn(err) end