Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

crap.json

JSON encode/decode functions. These are the same functions available as crap.util.json_encode / crap.util.json_decode, exposed under a dedicated namespace for convenience.

crap.json.encode(value)

Encode a Lua value (table, string, number, boolean, nil) as a JSON string.

local json = crap.json.encode({ name = "test", count = 42 })
-- '{"count":42,"name":"test"}'
ParameterTypeDescription
valueanyLua value to encode
ReturnsstringJSON string

crap.json.decode(str)

Decode a JSON string into a Lua value.

local data = crap.json.decode('{"name":"test","count":42}')
print(data.name)   -- "test"
print(data.count)  -- 42
ParameterTypeDescription
strstringJSON string
ReturnsanyDecoded Lua value

Common Patterns

Webhook Payload

crap.http.request({
    method = "POST",
    url = webhook_url,
    headers = { ["Content-Type"] = "application/json" },
    body = crap.json.encode({
        event = "new_inquiry",
        name = inquiry.name,
        email = inquiry.email,
    }),
})

Parse API Response

local resp = crap.http.request({ url = "https://api.example.com/data" })
if resp.status == 200 then
    local data = crap.json.decode(resp.body)
    crap.log.info("Got " .. #data .. " items")
end