How to access the data stored in JSON on the database?

0 votes

Using Corona: I have been struggling with this for over a day now. I have a json file in a collection in a database. I can get my document using its Id.


storageService:findAllDocuments(dbName, collectionName, App42CallBack)

function App42CallBack:onSuccess(object)

   for i=1, table.getn(object:getJsonDocList()) do

         local doc = object:getJsonDocList()[i]:getDocId()

   end

end


Great. I have my document. Now in my document's JSON file, there are values such as "level" = 4. How do I get 4? I need a detailed answer. I read about queries and such, but that only gets me the document all over again. I need the data inside the json file. In my debugger I see: Success is: My document information including my json data I have no idea how to get the data. Thanks.

asked Mar 26, 2016 in Corona by brettcomardelle93 (10 points)

1 Answer

0 votes

Hi,

Once you have the success response, you can find your JSON doc using the below code snippet from which you can your desired keys:

function App42CallBack:onSuccess(object)

   for i=1, table.getn(object:getJsonDocList()) do

         local doc = object:getJsonDocList()[i]:getDocId()

         local jsonDoc = object:getJsonDocList()[i]:getJsonDoc()

   end

end

 

Let me know if it helps.

 

Regards,

Himanshu Sharma

answered Mar 26, 2016 by hs00105 (2,005 points)
I get the document, but I try to print the table using ipairs, and nothing shows. If I try to decode the json file using (JSON:decode()) or (json.decode() --- corona use), I get an error "expected string but got table". If I just print(jsonDoc) it shows "table: 0CD6B8A8" and other tables like that.

"From which  you can get your desired keys" <--- how do I do that.

So, I have the document. Now, how do I get into that document to fetch the data. I need to see an example of how to return 4. The above just returns the document.
Download Widgets
Welcome to ShepHertz Product line forum, where you can ask questions and receive answers from the community. You can also reach out to us on support@shephertz.com
...