Skip to main content

json.decode()

Decode JSON text into Lua values.

Signature​

json.decode(text: string): any

Parameters​

ParameterTypeDescription
textstringJSON text to parse

Returns

The decoded value: a table for objects and arrays, or a string, number, boolean or nil.

Description​

Invalid JSON throws an error. Wrap network input in pcall() when the server may return something else, such as an HTML error page.

Example​

local response = Request("https://api.example.com/user/1"):get()
local ok, user = pcall(function()
return json.decode(response:getData())
end)
if ok then
toast("Hello " .. user.name)
end