Cannot access the downloaded JSON

I have a JSON that I am downloading from the internet. When I print the JSON using: 

data = json.decode(event.response) print(json.prettify(data))  

It gets printed as shown below

[{ "description: ":"This is the quiz Description. ", "day":"05", "quiz\_id":13, "image":"http://shektech.org/quizapp/2019/03/basket123.png", "year":"2019", "theme":"first quiz", "month":"03" },{ "Question 1":{ "type":"multi", "anstext":"This is the answer statement for questions 1 ", "correct":"1", "ansimage":"http://shektech.org/quizapp/2019/03/backicon.png", "qheading":"Question No 1", "questtext":"Answer: 1: Question Number 1 Statement: ", "qimage":"http://shektech.org/quizapp/2019/03/B\_1-1.png", "option1":"This is option 1", "hint":"The answer is 1.", "option4":"This is Option 4", "option2":"This is Option 2", "option3":"This is Option 3" } },{ "Question 2":{ "type":"multi", "anstext":"Question 2 Answer Statement: ", "correct":"4", "ansimage":"http://shektech.org/quizapp/2019/03/basket123.png", "qheading":"Question Number 2", "questtext":"Answer: 4: Statement Questions Number 1: ", "qimage":"http://shektech.org/quizapp/2019/03/B\_2.png", "option1":"This is Option 1", "hint":"The answer is option 4", "option4":"This is Option 4", "option2":"This is Option 2", "option3":"This is Option 3" } }]

I have trouble retrieving the values in the code. 

I tried the following code but it does not return the corresponding values:

print(data[0].quiz\_id)

Is there something wrong with the JSON or am I not accessing it correctly in a table.

Tables start at position 1 in lua, not 0.

Try changing 0 to 1.

print(data[1].quiz_id)

Thanks. I took help from the link “https://stackoverflow.com/questions/42139363/how-to-parse-json-in-lua” and solved it.

 data = json.decode(event.response) quizObject = json.encode(data) quizObject = string.sub(quizObject, 2, (string.len(quizObject)-1)) quizObject = string.gsub(quizObject, "},{", ",") quizObject = "return "..quizObject:gsub('("[^"]-"):','[%1]=') print(quizObject) quizObject = loadstring(quizObject)() print(quizObject.quiz\_id)

Another problem I have encountered a few times with JSON is when there is a gap in the indices, so the keys get explicitly named. 

Then when you use json.decode, it treats the keys as strings rather than numbers (e.g. quizObject[3] would fail but quizObject[“3”] would work.

When decoding json, I always have it attempt to convert all “numeric” string keys into number keys:

function json.toNumericKeys(t) local t2 = {} for k,v in pairs(t) do if type(v) == "table" then --recursive call if we have a table of tables json.toNumericKeys(v) end if tonumber(k) and type(k) ~= "number" then t2[tonumber(k)] = v else t2[k] = v end end return t2 end local quizObject = json.decode(data) quizObject = json.toNumericKeys(quizObject)

I get the credentials.json file when I download the client configuration files.

Hello and welcome to the Corona forums!

Can you provide more context about what you’re doing? There isn’t enough here to even begin to guess as to where to help you.

Read this post to help learn to ask better questions: 

https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

Rob

The error handler of an $.ajax request has the signature

Function( jqXHR jqXHR, String textStatus, String errorThrown )

[…] receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. TTRockStars

Change your function to

error: function(jqXhr) { var data = jqXhr.responseJSON; // you saw this in your console.log if (data) { … } else { // there might be other errors, where you don't get the server message } }

Hello and welcome to the Corona Labs forums. This is not the best place to ask JavaScript/JQuery questions. These forums are for people to discuss using Corona and Lua to build mobile apps and games.

You will find better answers else where. Perhaps try StackOverflow!

Rob

Tables start at position 1 in lua, not 0.

Try changing 0 to 1.

print(data[1].quiz_id)

Thanks. I took help from the link “https://stackoverflow.com/questions/42139363/how-to-parse-json-in-lua” and solved it.

 data = json.decode(event.response) quizObject = json.encode(data) quizObject = string.sub(quizObject, 2, (string.len(quizObject)-1)) quizObject = string.gsub(quizObject, "},{", ",") quizObject = "return "..quizObject:gsub('("[^"]-"):','[%1]=') print(quizObject) quizObject = loadstring(quizObject)() print(quizObject.quiz\_id)

Another problem I have encountered a few times with JSON is when there is a gap in the indices, so the keys get explicitly named. 

Then when you use json.decode, it treats the keys as strings rather than numbers (e.g. quizObject[3] would fail but quizObject[“3”] would work.

When decoding json, I always have it attempt to convert all “numeric” string keys into number keys:

function json.toNumericKeys(t) local t2 = {} for k,v in pairs(t) do if type(v) == "table" then --recursive call if we have a table of tables json.toNumericKeys(v) end if tonumber(k) and type(k) ~= "number" then t2[tonumber(k)] = v else t2[k] = v end end return t2 end local quizObject = json.decode(data) quizObject = json.toNumericKeys(quizObject)

I get the credentials.json file when I download the client configuration files.

Hello and welcome to the Corona forums!

Can you provide more context about what you’re doing? There isn’t enough here to even begin to guess as to where to help you.

Read this post to help learn to ask better questions: 

https://forums.coronalabs.com/topic/55780-ask-a-better-question-get-a-better-answer/

Rob

The error handler of an $.ajax request has the signature

Function( jqXHR jqXHR, String textStatus, String errorThrown )

[…] receives three arguments: The jqXHR (in jQuery 1.4.x, XMLHttpRequest) object, a string describing the type of error that occurred and an optional exception object, if one occurred. TTRockStars

Change your function to

error: function(jqXhr) { var data = jqXhr.responseJSON; // you saw this in your console.log if (data) { … } else { // there might be other errors, where you don't get the server message } }

Hello and welcome to the Corona Labs forums. This is not the best place to ask JavaScript/JQuery questions. These forums are for people to discuss using Corona and Lua to build mobile apps and games.

You will find better answers else where. Perhaps try StackOverflow!

Rob