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

Is it possible to have a JSON like table in RBX_Lua?

I was wondering if it would be possible, would be helpful in creating GUIs, and calling back on information. I know that there is a way for simple variables, but I was wondering if I could get a full on JSON table, instead of setting Variables inside an Array, or table.

local Table = {
	Name = "Generic"
	Age = 103
}

This is great, but I was wondering if I could could get it more in line with JSON documentation, this is from the json.org/example.html page.

"window": {
        "title": "Sample Konfabulator Widget",
        "name": "main_window",
        "width": 500,
        "height": 500
 }

This was the idea, to get it in this kind of syntax to be used in for loops, such as storing multiple string values in 1 variable.

Thanks in advanced.

2 answers

✓ Accepted answer

JSON (JavaScript Object Notation) is simply a string representation of the stored table. You cannot store a mixed table but you can nest them e.g:-

local a = {'a','b','c', {a = 'a2', b = 'b2', c = 'c2'}}  -- this is not a mixed table but a nested table

print(game.HttpService:JSONEncode(a))

-- will print
-- ["a","b","c",{"b":"b2","c":"c2","a":"a2"}] 

The encode and decode methods conform to the JSON format and will work as expected, note that JSONEncode and JSONDecode does not require the HttpService to be enabled.

JSON is commonly used for storing and sending structured data but it does have its limitations. It is also possible to send tables in remote events / functions but again they have limitations for mixed tables "A table passed with both an array part and dictionary part will lose the dictionary part".

I hope this helps.

1 comment

0

Also worth noting, the need to use JSON to encode/decode a table to/from data store is unnecessary, since ROBLOX will parse the data automatically. Just a tip for others who may read.

You can use JSONDecode and JSONEncode. They are built-in functions from httpService.

You can use either arrays or dictionaries.

local myArray = {
"My",
"Array",
"Is",
"Cool"
}

local myDictionary = {
["My dictionary"] = "Cooler"
}

HttpService = game:GetService("HttpService")

print(HttpService:JSONEncode(myArray), HttpService:JSONEncode(myDictionary))

Any questions? Post a comment.

Source: http://wiki.roblox.com/index.php?title=JSON

Edit: you might have to set HttpEnabled to true.

1 comment

0

JSON only parses it's string/table argument to it's appropriate encoding/decoding, HttpEnabled won't effect it.

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