Should I be encoding Data to JSON when Saving?

This is just a question I have about DataStores (again.)

I have noticed some DataStores use HttpService.JSONEncode() when Saving Data, and then using HttpService.JSONDecode() when Getting this Data.

I was mainly wondering the purpose for it, for some use cases I have seen, it can be used to compact Large Amounts of Data or “Save Instances” when Saving things, which seemed ideal, but Ive had some concerns that I was hoping would get cleared up.
And so It got me wondering:

  • Should I be doing this?

  • What would doing this actually do with my Data?

  • Would This make Data Storing easier and faster?

  • Would It help with DataStoreService?

  • Would It help with Data Stores in general?

There are some questions I have about it, you don’t have to answer them, If anything I just create Topic’s asking a bunch of Questions so, not sure.

Thanks for reading however.

2 Likes

I do this with my data stores, and I believe that it is efficient because it does compact large amounts of data, as you stated, which means less data saved to entries.

According to the developer documentation, “Along with request frequency, data stores limit how much data can be used per entry. The data store name, key name, and scope must all be under a certain character length, as well as the amount of data stored.”
The developer documentation also states, “Since the data store name, key name, and scope are strings, their length can be checked with string.len(). The data (key value) is also stored as a string, regardless of its initial type. The size of data can be checked with the JSONEncode() function that converts Lua data into a serialized JSON table.”

2 Likes

No. Roblox already does it for yo. You can only save one entry/dictionary and the same goes with encoding and decoding, therefore it’s already wrapped in a big table that Roblox will json encode… Also, JSON encoding can actually be less efficient since strings need to be backslash and double quotes. Now when roblox JSONEncode you JSONENCODe it too will backslash and double quotes for the backslashed doubled quotes.

Don’t do it, no point, roblox already does it for you.

6 Likes

oh, well thanks for the info, I’m not sure which is true or which isn’t so ¯\_(ツ)_/¯

Even if both of our statements were incorrect, I would just go with what the documentations say, since information related to your question was provided.

Let us say that @NovusDev is correct and Luau automatically encodes the data; let us also remember that the documentation states that there is a limit on the amount of data that can be saved. That means there is a limit on how much data Luau can encode. Why not encode the data using JSONEncode() to prevent that limit from being reached, resulting in Luau being able to pack up (encode) all of the data?

I’ve tried storing regular tables in the past, I don’t believe they are as reliable.

1 Like

It’s a character limit. and JSONEncoding data adds characters for storing strings. Such as a backslash and double quotes. Now since roblox server not lua the interpreter automatically jsonEncodes any data sent to be saved. So lets say your trying to store a table with two strings, data = {“Test”, “Test2”}, let’s save you are trying to save data it will be encoded automatically and saved as [“Test”, “Test2”] in the Roblox datastores, this is to ensure all data is already jsonEncoded so it can be saved to begin with. now if you jsonEncode it, you aren’t trying to save data but rather a jsonEncoded data, so your attempting to save [“Test”, “Test2”], now roblox won’t check if this is jsonEncoded and will reEncode it to ensure it meets their saving criteria. so now it will be saved as “[“Test1”,“Test2”]”. So you instantly cause more data to save.

2 Likes

Thanks for informing me, I appreciate you increasing my knowledge. :slightly_smiling_face:

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