Can i convert a string into a table?

I have a string like this “{“Item”, “Item”}” and i want to turn it into a table like this {“Item”, “Item”}

Can i do that?

1 Like

Why would i need to use HTTP service

Why would you not need to use HTTP service, is it not enough for you?

How do i use it to turn a string that LOOKS like a table into a table?

I sent it in my first reply, click

You sent the documentation, not wht function i need to use. Is it just json decode?

local HttpService = game:GetService("HttpService")

local jsonString = [[
{
	"message": "success",
	"info": {
		"points": 120,
		"isLeader": true,
		"user": {
			"id": 12345,
			"name": "JohnDoe"
		},
		"past_scores": [50, 42, 95],
		"best_friend": null
	}
}
]]

local data = HttpService:JSONDecode(jsonString)

if data.message == "success" then
	-- Since tab["hello"] and tab.hello are equivalent,
	-- you could also use data["info"]["points"] here:
	print("I have " .. data.info.points .. " points")
	if data.info.isLeader then
		print("I am the leader")
	end
	print("I have " .. #data.info.past_scores .. " past scores")

	print("All the information:")
	for key, value in pairs(data.info) do
		print(key, typeof(value), value)
	end
end

Just tells me “cant parse JSON”

Im using this:

local TestString = [[
{
	"modclass": = "object",
	"objectid": = "",
	"objectname": = ""

}
]]

Idk anything about JSON

local HTTPService = game.HttpService

local TestString = [[
{
"objectid":"",
"modclass":"object",
"objectname":""
}
]]

local parsed = HTTPService:JSONDecode(TestString)
print(parsed)

Also why do you need this code for? Can you explain more about what you want to achieve? Why do you have a table as a string in the first place

have you tried roblox ai for help?
it should be pretty simple like doing operation on string

i.e.:
make a table variable- newTable={}
get each word
add it to the new table

i think it should be possible this way

Making a thingy that lets u add mods to the game, i decided to use tables as a thing to tell the game what to do, and i decided to use strings as an input, and i needed a table from that string

Code
-- Your input string
local inputString = '{"Item1", "Item2"}'

-- Function to convert the string to a table
local function stringToTable(str)
    -- Remove the curly braces
    str = str:gsub("^%s*{(.-)}%s*$", "%1")
    
    -- Split the string by ", "
    local items = {}
    for item in string.gmatch(str, '"(.-)"') do
        table.insert(items, item)
    end
    
    return items
end

-- Call the function
local resultTable = stringToTable(inputString)

-- Print the results
for _, item in ipairs(resultTable) do
    print(item)  -- Output: Item1, Item2
end
String vs Http (In this scenario)

Method 1: Using HttpService:JSONDecode

  • How Fast It Is: It takes time based on how long the string is. If the string has 10 letters, it will take a little longer than if it has 5.
  • Memory Use: It uses some memory to hold the items it finds. The more items, the more memory it needs.
  • Good For: It works well for complicated strings with lots of data.

Method 2: Using String Operations

  • How Fast It Is: It also takes time based on how long the string is, just like the first method.
  • Memory Use: It uses memory for the items it finds too. Again, more items mean more memory.
  • Good For: This method is easier and works best for simple strings that aren’t too long.

Conclusion

  • For simple cases, string operations can be just as fast or faster because they involve less overhead.
  • For complex data, JSONDecode is likely faster overall since it’s optimized for parsing JSON.

In short, if you’re working with simple strings, string operations might be quicker, but for anything more complicated, JSONDecode is usually the better choice.

signed by (ChatGpt)

Idk, JSON decode is better in my opinion

1 Like

Why does the speed matter if you won’t convert a trillion strings to tables

1 Like

just a comparison between those two

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