Load .json files on Lua Tables.

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 – Is a valide json checked via jsonlint.com

  {         "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!

jsonFile() is some function someone wrote to read a file in from the operating system and store it into your local memory.  It’s not an official API call.

Look at something like this:

local function loadTable(filename)     local path = system.pathForFile( filename, system.DocumentsDirectory)     local contents = ""     local myTable = {}     local file = io.open( path, "r" )     if file then         --print("trying to read ", filename)         -- read all contents of file into a string         local contents = file:read( "\*a" )         myTable = json.decode(contents);         io.close( file )         --print("Loaded file")         return myTable     end     print(filename, "file not found")     return nil end

Rob

I know exactly the “jsonFile” function you’re talking about, but can’t find a reference to it now.  The copy I have in my code looks like this:

function jsonFile( filename, base ) -- set default base dir if none specified if not base then base = system.ResourceDirectory; end -- create a file path for corona i/o local path = system.pathForFile( filename, base ) print( "jsonFile ["..path.."]" ) -- will hold contents of file local contents -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) if file then -- read all contents of file into a string contents = file:read( "\*a" ) io.close( file ) -- close the file after using it else print( "\*\* Error: cannot open file" ) end print( "contents\<\<\<EOF") print( contents ) print( "\>\>EOF") return contents end

jsonFile() is some function someone wrote to read a file in from the operating system and store it into your local memory.  It’s not an official API call.

Look at something like this:

local function loadTable(filename) &nbsp;&nbsp;&nbsp; local path = system.pathForFile( filename, system.DocumentsDirectory) &nbsp;&nbsp;&nbsp; local contents = "" &nbsp;&nbsp;&nbsp; local myTable = {} &nbsp;&nbsp;&nbsp; local file = io.open( path, "r" ) &nbsp;&nbsp;&nbsp; if file then &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --print("trying to read ", filename) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; -- read all contents of file into a string &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; local contents = file:read( "\*a" ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myTable = json.decode(contents); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; io.close( file ) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; --print("Loaded file") &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; return myTable &nbsp;&nbsp;&nbsp; end &nbsp;&nbsp;&nbsp; print(filename, "file not found") &nbsp;&nbsp;&nbsp; return nil end

Rob

I know exactly the “jsonFile” function you’re talking about, but can’t find a reference to it now.  The copy I have in my code looks like this:

function jsonFile( filename, base ) -- set default base dir if none specified if not base then base = system.ResourceDirectory; end -- create a file path for corona i/o local path = system.pathForFile( filename, base ) print( "jsonFile ["..path.."]" ) -- will hold contents of file local contents -- io.open opens a file at path. returns nil if no file found local file = io.open( path, "r" ) if file then -- read all contents of file into a string contents = file:read( "\*a" ) io.close( file ) -- close the file after using it else print( "\*\* Error: cannot open file" ) end print( "contents\<\<\<EOF") print( contents ) print( "\>\>EOF") return contents end