How to take values on complex JSON tables?

Greetings,

I am dealing with social issues. I know the value of the decode JSON table, but now I have a problem.

local UserDataJson = json.encode( UserData, { indent = true } )

{

  “scriptData”: {

    “friendsList”: {

      “5ce69a4ecf0xxxxxx237d5d4”: {

        “displayName”: “Mark”

      },

      “5cexxxxxxdddc5237d664”: {

        “displayName”: “Peter”

      }

    }

  }

}

In the middle is the User ID, I used xxx to hide.

I used UserDataJson.friendsList to get a table reply.

I tried to use UserDataJson.friendsList[1] or UserDataJson.friendsList.displayName to get nil.

Is there a way to get the middle UID and displayName individually?

Thank you.

Have you tried to iterate over friendsList using in pairs? 

https://docs.coronalabs.com/api/library/global/pairs.html

Dear Nick,

I tried to use this method.

But it keeps appearing.

bad argument #1 to ‘pairs’ (table expected, got string)

Your JSON doesn’t make much sense to me.  Try

{ {"id":"5ce69a4ecf0xxxxxx237d5d4", "name": "Mark"}, {"id":"5cexxxxxxdddc5237d664": "name": "Peter"}, }

Sounds like you’re trying to iterate over the JSON, not the original Lua tables. You need to decode the JSON into a Lua table before you can dig into it.

Yes, you encode to save and decode to use.  Still I advise using a more normalised data structure.

I have decode it beforehand.

This table is from GameSparks CloudCode to Corona.

After decode, the result of print↓

{

 “friendsList”:{

  “5ce69a4ecf0xxxxxx237d5d4”:{

      “displayName”:“Mark”

     },

    “5cexxxxxxdddc5237d664”:{

        “displayName”:“Peter”

     }

    }

  }

But the system does not seem to recognize the value of friendsList.

That’s not decoded JSON, that’s a JSON string. Can you show the code where you are decoding?

getLocationRequest:send(function(response)
if response:hasErrors() then
–If errors then print errors
else
local FriendData = response:getScriptData()
local FriendJson = json.encode( FriendData, { indent = true } )
native.showAlert(“FriendData”, json.encode( FriendData, { indent = true } ), {“Ok”})
end
end)

You are using EN code not DE code.

Oh, indeed, because of the use of decode will produce nil.

Well, if the JSON string won’t decode that just means your JSON is malformed. You can’t encode a string and expect to be able to access data within it - it’s not a data structure that Corona understands.

What does response:getScriptData() do?

The result of response is the one posted at the top, and the same output result in Gamespark’s Test tool.

I just used decode but the result is the same as nil. :frowning:

I think I have to go to the hospital to undergo the suture after surgery.

Thank you all.

What happens if you try to decode the raw response?

I found the problem. !!!

Just in the hospital, I kept thinking about the method you provided.

   local FriendData = response:getScriptData()

   local FriendJson = json.encode( FriendData , { indent = true }

   local decoded, pos, msg = json.decode( FriendJson )

    if not decoded then

           print( "Decode failed at “…tostring(pos)…”: "…tostring(msg) )

    else

           print( decoded.friendsList )  – Get a table value

           for k,v in pairs( decoded.friendsList ) do

                 print( "UID: “…k…” | " )

                 for a,b in pairs( v ) do

                     print( "displayName: “…a…” | "…b )

                 end

           end

  end

If I only use in pairs once, the latter will get error.

Dear Nick, thank you for your guidance. :smiley:

Have you tried to iterate over friendsList using in pairs? 

https://docs.coronalabs.com/api/library/global/pairs.html

Dear Nick,

I tried to use this method.

But it keeps appearing.

bad argument #1 to ‘pairs’ (table expected, got string)

Your JSON doesn’t make much sense to me.  Try

{ {"id":"5ce69a4ecf0xxxxxx237d5d4", "name": "Mark"}, {"id":"5cexxxxxxdddc5237d664": "name": "Peter"}, }

Sounds like you’re trying to iterate over the JSON, not the original Lua tables. You need to decode the JSON into a Lua table before you can dig into it.

Yes, you encode to save and decode to use.  Still I advise using a more normalised data structure.