JSON
Parse and generate JSON data for configuration and API responses.
json.parse
Signature: json.parse(data)
Description: Parses a JSON-encoded string and returns a Lua table.
Parameters:
data (string): The JSON-encoded string to parse.
Returns:
table: A Lua table representation of the JSON data.
Example:
-- Example JSON string
local raw = '{"name": "Nightlings", "version": 1.0, "features": ["gui", "overlay", "lua"]}'
-- Parse JSON into Lua table
local tbl = json.parse(raw)
engine.log("Project: " .. tbl.name, 255, 255, 255, 255)
engine.log("Version: " .. tostring(tbl.version), 255, 255, 255, 255)
engine.log("First feature: " .. tbl.features[1], 255, 255, 255, 255)
json.stringify
Signature: json.stringify(lua_table)
Description: Converts a Lua table into a JSON-formatted string (with indentation).
Parameters:
lua_table (table): The Lua table to convert to JSON.
Returns:
string: A JSON-formatted string representing the Lua table.
Example:
-- Example JSON string
local raw = '{"name": "Nightlings", "version": 1.0, "features": ["gui", "overlay", "lua"]}'
-- Parse JSON into Lua table
local tbl = json.parse(raw)
engine.log("Project: " .. tbl.name, 255, 255, 255, 255)
engine.log("Version: " .. tostring(tbl.version), 255, 255, 255, 255)
engine.log("First feature: " .. tbl.features[1], 255, 255, 255, 255)
-- Modify the table
tbl.debug = true
tbl.features[#tbl.features + 1] = "json_api"
-- Convert back to JSON
local out = json.stringify(tbl)
engine.log("Updated JSON:", 255, 255, 255, 255)
engine.log(out, 255, 255, 255, 255)
Last updated
Was this helpful?