util.table.encode

Shared

Encodes a table into a JSON string


Syntax

local result = util.table.encode(input)

Parameters

TypeNameDescription
tableinputTable to encode

Returns

TypeNameDescription
string | boolresultEncoded JSON string on successful execution, false otherwise

Examples

Encode a simple table to JSON
local data = {name = "John", age = 25}
local result = util.table.encode(data)

core.engine.print("info", result) -- '{"name":"John","age":25}'
Encode an array to JSON
local colors = {"red", "green", "blue"}
local result = util.table.encode(colors)

core.engine.print("info", result) -- '["red","green","blue"]'
Encode a numeric array to JSON
local coords = {100, 200, 300}
local result = util.table.encode(coords)

core.engine.print("info", result) -- '[100,200,300]'
Encode boolean values to JSON
local settings = {enabled = true, debug = false}
local result = util.table.encode(settings)

core.engine.print("info", result) -- '{"enabled":true,"debug":false}'

On this page