JSON Implementation - Attempt to call global ‘jsonFile’ (a nil value)

Hello there,

I used this tutorial

http://www.coronalabs.com/blog/2011/08/03/tutorial-exploring-json-usage-in-corona/

and I’m getting the error “Attempt to call global ‘jsonFile’ (a nil value)”

I know that there are working JSON solutions out there, so what is wrong with that?

local path local source local t = {} [...] local Data = saveData:read("\*a") t = json.decode(Data) io.close(saveData)

After I say print(t.abc) whatever I always says that t is nil.

I just wonder what’s wrong with that?

Can anybody help?

Did you add:

[lua]local json = require( “json” )[/lua]

to the top of the file?

What’s saveData?

We won’t magicaly guess what is each variable and what it does. You must paste all essential parts for us to help you.

Yes, I did.

My path is wrong I think,

How can I tell

local path =

the local project directory?

Does path = system.pathForFile(src, system.ResourceDirectory) work? I need something like this.

(Sorry about the code, wanted to focus on the important part, but I see you need all of it)

Here is the entire part:

local json = require("json") local src = "test.json" local t = {} local temp local path -- path = system.pathForFile(src, system.DocumentsDirectory) -- path = system.pathForFile(src, system.TemporaryDirectory) path = system.pathForFile(src, system.ResourceDirectory) local saveData = io.open(path, "r") local Data = saveData:read("\*a") t = json.decode(Data) io.close(saveData) print(t.abc) ----------------- test.json looks like this: { {"abc":"hello"} }

Any ideas?

Can’t no one help me with this?

You could also give me a working sample, please.

I don’t see anything obviously wrong with the code you pasted in your last post.  Are you still getting an error?  If so, what is it?

The tutorial blog post I think incorrectly calls a function jsonFile that doesn’t exist.  So if you worked off the tutorial directly, that would explain the error in your original post.

Since your new post (correctly) doesn’t call a non-existent function jsonFile, any error you’re getting now must be different.

  • Andrew

 attempt to index local ‘t’ a nil value

Do you think, that print(t.abc) is wrong or my .json file is buggy?

Seems like the data is loaded, but can’t use it :frowning:

As error states, “t” is nil —> jsonData is invalide so json.decode() fails

–EDIT

 json file must be

{ "abc":"hello" }

Thank you for your advice :).

I found the error. Loaded the sample.json and compared to my own .json file.

The syntax of my abc.json file was wrong.

My code was working at all.

Edit: Ah, now you’ve seen it, too. :slight_smile: Thanks.

For future reference, if you’re editing JSON data by hand, it’s a good idea to use a validator like JSONLint (http://jsonlint.com/) to check that you don’t have any syntax errors.

  • Andrew

Thanks, didn’t thought that for .JSON there is something like that. Used something similar always for HTML :).

If you are using editor like notepad++ or sublime text then there are also plugins for friendly formatting json files, if it will help you :slight_smile:

Did you add:

[lua]local json = require( “json” )[/lua]

to the top of the file?

What’s saveData?

We won’t magicaly guess what is each variable and what it does. You must paste all essential parts for us to help you.

Yes, I did.

My path is wrong I think,

How can I tell

local path =

the local project directory?

Does path = system.pathForFile(src, system.ResourceDirectory) work? I need something like this.

(Sorry about the code, wanted to focus on the important part, but I see you need all of it)

Here is the entire part:

local json = require("json") local src = "test.json" local t = {} local temp local path -- path = system.pathForFile(src, system.DocumentsDirectory) -- path = system.pathForFile(src, system.TemporaryDirectory) path = system.pathForFile(src, system.ResourceDirectory) local saveData = io.open(path, "r") local Data = saveData:read("\*a") t = json.decode(Data) io.close(saveData) print(t.abc) ----------------- test.json looks like this: { {"abc":"hello"} }

Any ideas?

Can’t no one help me with this?

You could also give me a working sample, please.

I don’t see anything obviously wrong with the code you pasted in your last post.  Are you still getting an error?  If so, what is it?

The tutorial blog post I think incorrectly calls a function jsonFile that doesn’t exist.  So if you worked off the tutorial directly, that would explain the error in your original post.

Since your new post (correctly) doesn’t call a non-existent function jsonFile, any error you’re getting now must be different.

  • Andrew

 attempt to index local ‘t’ a nil value

Do you think, that print(t.abc) is wrong or my .json file is buggy?

Seems like the data is loaded, but can’t use it :frowning:

As error states, “t” is nil —> jsonData is invalide so json.decode() fails