The problem here is that the decode
function calls error
if it encounters an error.
This is Lua's equivalent to an exception handling mechanism. What you want to do is call the decode
function in protected mode:
local success, res = pcall(json.decode.decode, json_str);
if success then
-- res contains a valid json object
...
else
-- res contains the error message
...
end