Scripting HelpersARCHIVE
A little piece of Roblox history. Preserved for everyone, one question at a time.Read about the archive →
✓ ANSWER ACCEPTED

JSONDecode Returning nil constantly, thoughts?

Long story short, I'm making a profile "viewer" for fun using the Bungie.net API and all of the JSON data I decode is returning nil. I'll show you the JSON String I receive, as well as the code I am using to decode it into a table.

JSON String Returned:

{"Response":[{"iconPath":"/img/theme/destiny/icons/icon_psn.png",
"membershipType":2,"membershipId":"4611686018433631548",
"displayName":"xNirdx"}],"ErrorCode":1,
"ThrottleSeconds":0,"ErrorStatus":"Success",
"Message":"Ok","MessageData":{}}

My Code (In a ModuelScript):

-- bungie's servers reject any requests without an api key, just a warning in case you try to test this
httpService = game:GetService("HttpService");
headers = {
	["X-API-Key"] = "a1b2c3d4e5f6g7h8i9j" -- replaced, not supposed to share it publicly
};

function searchPlayer(username)
	local searchResult = httpService:GetAsync(
		"http://www.bungie.net/Platform/Destiny/SearchDestinyPlayer/" .. "TigerPsn/" .. username .. "/",
		true,
		headers
	);
	
	local decodeResults = httpService:JSONDecode(searchResult);
	print(decodeResults.Response.iconPath);
end

return searchPlayer;

Output:

nil

I'm absolutely stumped, and I'm sure there's a simple solution and I'm just overthinking it. Any help would be appreciated, and if more info is needed I'm happy to give it.

1 answer

✓ Accepted answer

The problem is how you are reading the newly created table. As shown by the second diagram an array is represented using '[]'.

You currently are accessing it as an object:-

print(decodeResults.Response.iconPath);

You need to access the arrays 1st index:-

print(decodeResults.Response[1].iconPath);

I hope this helps.

1 comment

0

I knew I just needed some sleep, lol. I'll try this after school, thank you!!

This discussion is preserved as it was. New answers, comments, and votes are closed.