Skip to main content

JSON

The json library provides functionality for encoding values to JSON and decoding values from JSON.

The json library is available for all UCMs, both hardware and software.

json.encode

-- @param value string|number|boolean|table|nil
-- @returns string|nil
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

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

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

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 © 2025 Enapter AG.