Turn string into table

  1. What do you want to achieve? Hello, i have a string like this : {Type = “Test”, Amount = 20} and i want to turn it into a table like that
Type = "Test",
Amount = 20
  1. What is the issue? I can’t find how to do it

  2. What solutions have you tried so far? I looked on forums but what i found there put numbers in front of the Type and Amount

It would be easiest if the string was encoded as JSON because you can use HttpService and turn it into a table

How can i turn the string into JSON?

Like this?

"Type": "Test",
"Amount": = 20
local HttpService = game:GetService("HttpService")

local myTable = {
	Type = "Test",
	Amount = 20

local myTableEncoded: string = HttpService:JSONEncode(myTable)
-- {"Amount":20,"Type":"Test"}

local myTableDecoded: typeof(myTable) = HttpService:JSONDecode(myTableEncoded)
-- {
--     ["Amount"] = 20,
--     ["Type"] = "Test"
-- }

I use GameAnalytics RemoteConfigs for the system i am doing and basically it returns the string like {"Type": = "Neutrons","Amount": = 20} but when i try to decode directly it says it can’t parse JSON

I don’t really use that, but I can say It’s not returning a proper JSON format for some reason (keeping the = operator). If it’s absolutely not returning a proper json, you can always string.gsub to replace that pattern.

local response: string -- your returned string
response = string.sub(response, " = ", "")

Well i can control a bit what it returns, what i told it to return is {"Type": = "Neutrons","Amount": = 20} and it return it as a string, so what do i do with the string.sub response, do i put it in the JSONDecode?

Yeah then it should be a proper table. Problem is that the string being returned is not a proper JSON (with extra " = "). Of course, it’d be better if you returned a proper JSON in the first place instead of a malformed one to avoid any unwanted regex operations.

Oh wait i just found the issue, your JSONDecode was working good, it was just that i typed the table wrong in GameAnalytics, thank you!

This topic was automatically closed 14 days after the last reply. New replies are no longer allowed.