Skip to main content

JSON

json.encode

-- @param value string,table,number
-- @returns string
function json.encode(value)
end

Encode value to json string.

Example

local json_string = json.encode({ counter = 1 })
enapter.log("json_string: " .. json_string)
-- json_string: {"counter":1}

json.decode

Decodes JSON string to Lua value. Returns error string as second return parameter in case of decoding error.

-- @param value string
-- @returns string,table,number error
function json.decode(value)
end

Example

local value, err = json.decode('{"counter":1}')
if err == nil then
enapter.log("value: " .. inspect(value))
end
-- value: {
-- counter = 1
-- }

local value, err = json.decode('invalid json')
if err then
enapter.log("Failed to decode json string: " .. err, "error")
end
-- Failed to decode json string: ... unexpected character 'i' at line 1 col

All Rights Reserved © 2024 Enapter AG.