JSONEncode and JSONDecode Questions

Hello! Today I was making a game where you “stalk” other players by searching their name and finding their followers, following, account age, etc… Could someone explain JSONE and JSON to me?

You can read a lot about them on the devhub:

It’s mainly used for Http requests that need to be in a JSON format/come in through JSON format. Most API’s these days need any information sent out to be in JSON, and will return a response in JSON, which then needs to be Decoded to Lua. I highly recommend reading the documentation I sent.

JSON literally stands for JavaScript Object Notation.

JSON is a data format commonly used to pass data to/from web endpoints. Think of it as the contents you put in an envelope that you then mail. The “language” of your mail is JSON. We keep that consistent so that everyone sending/receiving can understand it. It is also sometimes used to store data, since it’s just a convenient way to serialize/deserialize data pretty quickly.

When sending/receiving data from HttpService, data is commonly structured in JSON. Therefore, we need to translate to/from Lua tables to/from JSON. We can use the JSONEncode and JSONDecode methods to do this translation for us.

For instance, the following is a Lua table, but would make no sense to any web API:

local data = {
   hello = "hello there"
}

So we can translate it to JSON so that web APIs can read it too:

local data = {hello = "hello there"}
local json = HttpService:JSONEncode(data)
print(json)
-- Will print this:  {"hello": "hello there"}

You could then use that data to send in the body of an HTTP request. This all works in the other direction too. When you receive data from an HTTP endpoint, you might need to use JSONDecode to translate it from JSON to Lua.

Check out the W3Schools tutorial for more info.

9 Likes

Thank you so much! So I can get everything FROM Thesr urls that I would use. All I would have to do is jsonencode my scripts then get my data and jsondecode back. Then I can find what I want correct? :smiley:

well if the said URL does infact return JSON data then you will have to decode it into a lua table:

local url;
local data = game:GetService("HttpService"):GetAsync(url)
print(game:GetService("HttpService"):JSONDecode(data)   -- will print the hxd of table