Can't parse JSON error

Hi guys, I’m trying to get an admin table from Pastebin (I wanted to use Pastebin because I wanted to be able to update admins without having to go in studio and shutting down the game, or using a datastore to admin/unadmin people)

I’m relatively new to how JSON works, so it’s entirely possible I formatted it incorrectly. Here’s my Pastebin: Bordr Admins - Pastebin.com

My resources:

https://www.json.org/json-en.html
Examining code from Void's Anti-Scam [Open Source]

My code:

local HttpService = game:GetService('HttpService')
local BaseUrl = 'https://pastebin.com/raw.php?i='
local PastebinId = '5GtkaSUB'


local Admins = HttpService:GetAsync(BaseUrl..PastebinId)
print(Admins)

local AdminTable = HttpService:JSONDecode(Admins)

for i,v in pairs(AdminTable) do
	print('Name: '..v.Name..'\nUserId: '..v.Id..'\nTitle: '..v.Title)
end

print(Admins) works correctly and prints the pastebin, but when I attempt to call JSONDecode on Admins it says it can’t parse the JSON.

1 Like

Roblox can’t parse object fields in JSON. You need to use array fields.

I’m assuming @Sharksie is correct but I find that kind of silly so just in case:
Are you sure the JSON is valid? I tried decoding it with JavaScript and it doesn’t work there, either. I think it’s the single quotes, shouldn’t they be double quotes (“User”)?

I’m confused, because the sample code on the wiki works.

Is that not an object field?

Try replacing the first and last characters with [] (instead of {}).

[] is used for creating an array, and {} is used for creating an object. Your JSON is an array, but you’re telling the parser that it’s an object.

Edit: If this alone doesn’t work, try changing the strings to use double quotes (as mentioned above).

In roblox lua, ’ works as a substitute for ". I’m not sure about JavaScript.

In any case, I’ll attempt to convert the ’ on the pastebin into " and see if that makes any difference.

It’s fine in JavaScript as well, but JSON isn’t JavaScript - JSON is very specific.
There’s also another issue, the trailing commas after all the Title fields have to be removed. That’ll also throw off the parser.

Should be fine after those two changes

Edit: Also, like Realbighead said, you’d need to change the first { and the last } into [ and ], respectively. Missed that one

4 Likes

When you say trailing commas after the title fields, do you mean these?image

1 Like

No, it would be the ones here:

'Title': 'Head Admin',

Because it’s the last field of your User object, it can’t have a trailing comma there - so you’d have to switch that over to:

		"User": {
			"Name": "ChiefConstable2000",
			"Id": 242925792,
			"Title": "Head Admin"
		}

Otherwise the JSON parser will throw an error that it’s expecting another field there

Oh, okay. Usually when I do tables in Lua it doesn’t care if I leave a comma where it’s not needed, it’d still work fine. I’ll try that and let you know!

1 Like

Yeah, Lua’s really lenient with stuff. Same with JavaScript. But JSON I think doesn’t really expect them to be user created (just - JSON Stringify, JSON Parse) so it expects things to be exact

I am making progress. Now I’m getting a new error?

Line 12:

print('Name: '..v.Name..'\nUserId: '..v.Id..'\nTitle: '..v.Title)

In your JSON, you have to index the “User” key to get the name, id, and title. Just replace v.Name with v.User.Name (and do the same for id and title too)

2 Likes

Whoops, that’s a big mistake on my part. I should of caught that.

Thank you!