Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

How do you print a value in a decoded json table?

Asked by
VRGamez 27
7 years ago

Hello, I'm trying to get the name of the most popular game on the front page at any time, but for some reason it returns nil

local http = game:GetService("HttpService")
local httptable = http:GetAsync("https://rprxy.xyz/games/list-json?sortFilter=1&MaxRows=1")
local decoded = http:JSONDecode(httptable)
print(decoded.Name)

any help would be appreciated

1 answer

Log in to vote
0
Answered by 7 years ago

Hey VRGamez,

The issue that you are having is quiet simple to solve as well. You see, when the JSON Table is decoded into Lua, it looks something like this.

If you can tell from that image, there's a table with 1 value inside of it, and that value is the table wihich, would most commonly be refered to as a 'dictionary'. Basically, a dictionary because it's indexes aren't numbers like common tables but, rather Strings. So, to get the name from the dictionary you'd simply do:

dictionary.Name;

As you've done in your script. However, the one fatal mistake you've made in your script is you forgot to consider that there might actually be a dictionary inside of this table. With a bit more testing you would've found that fact out sooner. So, anyways, getting to your solution you simple have to get the dictionary inside of the table, and since it's quiet clear that there's only 1 dictionary, you can just index it with the number '1'. Below I'll show you how you can do this with my own code.

local http = game:GetService("HttpService")
local httptable = http:GetAsync("https://rprxy.xyz/games/list-json?sortFilter=1&MaxRows=1", true)
local decoded = http:JSONDecode(httptable)

print(decoded[1].Name) -- Gets the dictionary by indexing it from the table. Then, it gets the game's name by just using .Name index on the dictionary.

Well, that's that. I hope I helped and have a wonderful day/night.

~~ KingLoneCat

0
Thank you for your help. Your explanation about dictionaries helped me to better understand it. VRGamez 27 — 7y
0
Thanks KingLoneCat 2642 — 7y
Ad

Answer this question