Manipulation of JSON data strings.
Decode a string of JSON data into a Lua table. A Lua error is raised for syntax errors.
json - json data
options - table with decode options
decode_null_as_userdata
: wether to decode a JSON null value as json.null or nil (default is nil)data - decoded json
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
{
persons = {
1 = {
name = John Doe,
}
2 = {
name = Darth Vader,
}
}
}
Encode a lua table to a JSON string. A Lua error is raised for syntax errors.
tbl - lua table to encode
options - table with encode options
encode_empty_table_as_object
: wether to encode an empty table as an JSON object or array (default is object)json - encoded json
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
{"persons":[{"name":"John Doe"},{"name":"Darth Vader"}]}
Represents the null primitive from a json file