A Table returns nil even though there's a value

Hello!
So, I’m connecting my game to Trello and a list in it.
Each card has its description.
And this description is a table.
For example like that local tableTest = {["Test1"]="123",["Test2"]="456"}.
When I try to print some value like print(tableTest[“Test1”]), it works fine but when I try to do it in a for i loop (which loops through cards, I get nil).
If I print card’s description I get the same table as above, but when I try to get a value from it, it’s just nil.
Do you know how would I fix that?

Thank you!

EDIT: Noticed, that it returns as a string. Is there a way to make it as a table?

1 Like

I’m guessing it may be in JSON because it’s in string-form?
You could try decoding it into a table:
local Data = game:GetService("HttpService"):JSONDecode(string)

If I print it, it’s same as above.
Tried both encode and decode, but it’s nil.

Alright.
I tried looping through what you provided:
image

local tableTest = {["Test1"]="123",["Test2"]="456"}

for i,v in pairs(tableTest) do
	print(i,v)
end

I tried looping through it, and I get, that it’s not a table but a string.

Can you show the loop code snippet?

Well, I’ve tried multiple options,this is one of them

for i,v in pairs(TrelloAPI:GetCardsInList(List)) do
			local tableDesc = {}
			table.insert(tableDesc,v.desc)
			local json = HttpService:JSONEncode(v.desc)
			print(json)
			local jsonDec = HttpService:JSONDecode(json)
			print(v.desc)
			for key,val in pairs(jsonDec) do
				print(val)
			end
		end

Does it output anything? Also, what Trello API Module are you using?

https://www.roblox.com/library/214265621/Trello-API-Original

Well, this:

  "{[\"Test1\"]=\"123\",[\"Test2\"]=\"456\"}"
  {["Test1"]="123",["Test2"]="456"}```

for i loop errors "table expected, got string"

Have you done this before attempting to get the cards in the list?
TrelloAPI:GetListID("ListNameHere",boardid)

Yes, it returns v.desc normally as I provided it.

Seems like TrelloAPI:GetCardsInList() returns a string not a table. Therefore you can’t iterate trough it.

Okay.

for i,v in pairs(TrelloAPI:GetCardsInList(List)) do
			local tableDesc = {}
			table.insert(tableDesc,v.desc)
			local jsonDec = HttpService:JSONDecode(json)
			print(v.desc)
			for key,val in pairs(jsonDec) do
				print(val)
			end
		end

Try this, I noticed that in your previous code snippet, you encoded it and then decoded it, which didn’t do much.

json variable is missing … JSONDecode(json)

It returns “Can’t parse JSON” if I replace “json” with “v.desc” for example.

In your case you want to declare a dictionary which its syntax is :

local dictionary = {
["Test1"] = 5,
["Test2"]  = 2
} 
print(dictionary["Test1"]

In Roblox, it works fine, but, if I do it from Trello, it returns a string instead of the table.

What should I do now?
I tried putting a json encode string on trello and then decoding it and still same thing.

I looked for something similar to your issue.
Does this help? Getting data from trello to insert to a table - #2 by Dekkonot

I’ve solved the issue. Basically, Json was encoding a string instead of a table, so it should be like that: {"Test1":"123","Test2":"456"} and if you encode that, you can use it as a table.

1 Like