0

Basically i want to convert a .json file, into a Lua table, i'm using this tutorial ( probably out of date ), but i'm getting the following error:

attempt to call global 'jsonFile' (a nil value)

menu.lua

local json = require ("json")
local tableJson = json.decode( jsonFile("teste.json") )

teste.json

{
    "name": "Jack (\"Bee\") Nimble", 
    "format": {
        "shape":       "rect", 
        "width":      1920, 
        "height":     1080, 
        "interlace":  false, 
        "framerate": 24
    }
}

I have looked for "jsonFile", on the official API reference, but there's nothing there, and i did not found any way to do this.

Thanks in advance for the help!

André Oliveira
  • 1,105
  • 4
  • 20
  • 53

2 Answers2

1

json.decode receives a string so you probably need to read the contents of the file. Try this:

function jsonFile(file)
    local f,err = io.open(file, "r")
    if f==nil then
        return f,err
    else
        local content = f:read("*all")
        f:close()
        return content
    end
end
lhf
  • 70,581
  • 9
  • 108
  • 149
0

Use json.decodeFile function to decodes the contents of a file that is expected to contain JSON-encoded data.

ldurniat
  • 1,687
  • 2
  • 9
  • 16