util.table.decode

Shared

Decodes a JSON string into an equivalent table


Syntax

local result = util.table.decode(input)

Parameters

TypeNameDescription
stringinputJSON string to decode

Returns

TypeNameDescription
table | boolresultDecoded table on successful execution, false otherwise

Examples

Decode a JSON object string into a table
local json = '{"name":"John","age":25}'
local data = util.table.decode(json)

core.engine.print("info", data.name) -- 'John'
core.engine.print("info", data.age) -- 25
Decode a JSON array string into a table
local json = '["red","green","blue"]'
local colors = util.table.decode(json)

core.engine.print("info", colors[1]) -- 'red'
core.engine.print("info", colors[2]) -- 'green'
Decode a JSON number array
local json = '[100,200,300]'
local values = util.table.decode(json)

core.engine.print("info", values[1], values[2], values[3]) -- 100  200  300
Decode JSON boolean values
local json = '{"enabled":true,"debug":false}'
local settings = util.table.decode(json)

core.engine.print("info", settings.enabled) -- true
core.engine.print("info", settings.debug) -- false

On this page