JSON API documentation

Version: stable

FUNCTION
json.decode() decode JSON from a string to a lua-table
json.encode() encode a lua table to a JSON string
CONSTANT
json.null null

Functions

json.decode()

json.decode(json,options)

Decode a string of JSON data into a Lua table. A Lua error is raised for syntax errors.

PARAMETERS

json string json data
options table table with decode options
  • bool decode_null_as_userdata: wether to decode a JSON null value as json.null or nil (default is nil)

RETURNS

data table decoded json

EXAMPLES

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,
    }
  }
}

json.encode()

json.encode(tbl,options)

Encode a lua table to a JSON string. A Lua error is raised for syntax errors.

PARAMETERS

tbl table lua table to encode
options table table with encode options
  • string encode_empty_table_as_object: wether to encode an empty table as an JSON object or array (default is object)

RETURNS

json string encoded json

EXAMPLES

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"}]}

Constants

json.null

Represents the null primitive from a json file