Vital.sandbox
Table

table.encode

Shared

Encodes a table into string


Syntax

local result = table.encode(input, mode = "JSON")

Parameters

TypeNameDescription
tableinputTable to be encoded
stringmodeEncoding mode:
"JSON" - JavaScript Object Notation
"YAML" - YAML Ain't Markup Language

Returns

TypeNameDescription
string / boolresultEncoded string on successful execution, or false on failure

Examples

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

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

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

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

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

On this page