JSON
Must be included via require
.
json.encode
Returns a string of JSON.
Parameters
data
— A boolean, number, string, or table to encode as JSON.pretty
— Whether to return a human-readable string of JSON. Defaults tofalse
.
local json = require("json")
local data
data = "Hello, World!"
print(json.encode(data, true))
--> "Hello, World!"
data = {
key = "Hello, World!",
nested = {
nested_key = 1337
}
}
print(json.encode(data, true))
--> {
--> "key": "Hello, World!",
--> "nested": {
--> "nested_key": 1337
--> }
--> }
Because Lua tables do not have order guarantees, you can add an __order
field to fix the order:
local t = {
__order = { "a", "b", "c" },
a = 1,
b = 2,
c = 3
}
print(require"json".encode(t)) -- {"a":1,"b":2,"c":3}
The json.null
value — assuming you did local json = require("json")
— can be used to encode JSON null values.
local json = require("json")
print(json.encode(json.null)) --> null
json.decode
Returns multiple potential types. If you pass a serialized boolean, number, or string, then it will return the same type. If you pass a complex JSON object, it will return a table.
Parameters
data
— The JSON data to decode.flags
— Options to augment the return value. Multiple options can be combined with bitwise OR (|
). Defaults to0
.json.withnull
— decodes JSON null values asjson.null
instead ofnil
.json.withorder
— adds an__order
field to tables of decoded JSON objects.json.encode
respects this, so this is perfect for modifying data while preserving order.
local json = require("json")
local data, encoded, decoded
-- Basic Type
data = "Hello, World!"
encoded = json.encode(data, true)
decoded = json.decode(encoded)
assert(decoded == data)
assert(type(decoded) == "string")
-- Complex Type
data = {
key = "Hello",
nested = {
nested_key = 1337
}
}
encoded = json.encode(data, true)
decoded = json.decode(encoded)
assert(decoded.key == "Hello")
assert(type(decoded) == "table")
assert(decoded.nested.nested_key == 1337)
-- Flags
encoded = [[{
"null": null,
"string": "Hello"
}]]
decoded = json.decode(encoded, json.withnull | json.withorder)
assert(decoded.__order[1] == "null")
assert(decoded.null == json.null)
assert(json.encode(decoded, true) == encoded)