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 (json.decode_options) (optional) - optional decoding optionsReturns
data (any) - decoded JSON valueExamples
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: STRUCT JSON decoding options
Members
decode_null_as_userdata? (boolean) - Decode JSON null as json.null instead of nil.Type: FUNCTION Encode a lua table to a JSON string. A Lua error is raised for syntax errors.
Parameters
tbl (any) - Lua value to encodeoptions (json.encode_options) (optional) - optional encoding optionsReturns
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: STRUCT JSON encoding options
Members
encode_empty_table_as_object? (boolean) - Encode an empty table as an object instead of an array. The default is true.Type: VARIABLE Represents the null primitive from a json file