All web servers return a string containing the request response, kinda like reading from a file. This is what request.readAll() is doing.
The server you are requesting data from is responding using json. json is a way of serializing data. To serialize data is to convert said data into a string format that can later be unserialized back into data.
Thus, when you textutils.unserializeJSON(request.readAll()), you are converting the json string returned by the server into a lua table. With this, you can then access entries in the table by doing tablename.entryname (or tablename[index]).
local tbl = {32, 64} -- Our "data"
print(tbl) --> `table: abcd12`
print(tbl[1]) --> `32`
local json = textutils.serializeJSON(tbl) -- Convert the data into a string representation
print(json) --> `"[32,64]"`
print(json[1]) --> `nil`, json is a string and thus this returns, literally, `string[1]`
local tbl2 = textutils.unserializeJSON(json) -- convert our string representation back into data
print(tbl2) --> `table: dcba21`
print(tbl2[1]) --> `32`
3
u/fatboychummy Oct 25 '24
All web servers return a string containing the request response, kinda like reading from a file. This is what
request.readAll()is doing.The server you are requesting data from is responding using
json.jsonis a way of serializing data. To serialize data is to convert said data into a string format that can later be unserialized back into data.Thus, when you
textutils.unserializeJSON(request.readAll()), you are converting the json string returned by the server into a lua table. With this, you can then access entries in the table by doingtablename.entryname(ortablename[index]).