0

When I execute this code (Windows 10) i get an error from within the library.

local json = loadfile("json.lua")()

local handle = io.popen("curl \"https://someurl.com\"")
local result = handle:read("*all")
handle:close()

local output = json:decode(result)

The error in the console:

lua: json.lua:377: expected argument of type string, got table
stack traceback:
        [C]: in function 'error'
        json.lua:377: in method 'decode'
        monitor.lua:10: in main chunk
        [C]: in ?

I'm running the code on Windows 10 with a console and using this library: https://github.com/rxi/json.lua

This function always returns the same error, even if I try different types of arguments, i.e. numbers or strings.

function json.decode(str)
  if type(str) ~= "string" then
    error("expected argument of type string, got " .. type(str))
  end
  local res, idx = parse(str, next_char(str, 1, space_chars, true))
  idx = next_char(str, idx, space_chars, true)
  if idx <= #str then
    decode_error(str, idx, "trailing garbage")
  end
  return res
end
1
  • Replace json:decode with json.decode Commented Jan 8, 2022 at 17:07

1 Answer 1

1
local output = json:decode(result)

is syntactic sugar for

local output = json.decode(json, result)

json is a table.

Hence inside function json.decode the following if statement is entered:

if type(str) ~= "string" then
    error("expected argument of type string, got " .. type(str))
end

which produces the observed error.

To fix this you can either change the function definiton to

function json:decode(str)
  -- code
end

Or you call

local output = json.decode(result)

You should pick the second one as changing the json library will affect code that already uses json.decode as intended by the author.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.