JSON

Namespace: json Language: Lua Type: Defold Lua File: script_json.cpp Source: engine/script/src/script_json.cpp

Manipulation of JSON data strings.

API

json.decode

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

Parameters

Returns

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

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

Parameters

Returns

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

json.null

Type: VARIABLE Represents the null primitive from a json file