Namespace: json
Language: Lua
Type: Defold Lua
File: script_json.cpp
Source: engine/script/src/script_json.cpp
Manipulation of JSON data strings.
Type: FUNCTION Decode a string of JSON data into a Lua table. A Lua error is raised for syntax errors.
Parameters
json (string) - json dataoptions (table) (optional) - table with decode optionsdecode_null_as_userdata: wether to decode a JSON null value as json.null or nil (default is nil)Returns
data (table) - decoded jsonExamples
Converting a string containing JSON data into a Lua table:
function init(self)
local jsonstring = '{"persons":[{"name":"John Doe"},{"name":"Darth Vader"}]}'
local data = json.decode(jsonstring)
pprint(data)
end
Results in the following printout:
{
persons = {
1 = {
name = John Doe,
}
2 = {
name = Darth Vader,
}
}
}
Type: FUNCTION Encode a lua table to a JSON string. A Lua error is raised for syntax errors.
Parameters
tbl (table) - lua table to encodeoptions (table) (optional) - table with encode optionsencode_empty_table_as_object: wether to encode an empty table as an JSON object or array (default is object)Returns
json (string) - encoded jsonExamples
Convert a lua table to a JSON string:
function init(self)
local tbl = {
persons = {
{ name = "John Doe"},
{ name = "Darth Vader"}
}
}
local jsonstring = json.encode(tbl)
pprint(jsonstring)
end
Results in the following printout:
{"persons":[{"name":"John Doe"},{"name":"Darth Vader"}]}
Type: VARIABLE Represents the null primitive from a json file