JSONEncoding returns a weird string?

Is it normal to encode a string w/JSONEncode and have it output something wonky like:
https://hastebin.com/caqujujeje.bash

Is there a reason for this?

I’m trying to decode a players quests so I can save it since it contains something that’s not a valid utf-8 character. I’m not exactly sure what is going on here.

Saving what quests equals as a string:

local encoded2 = game.HttpService:JSONEncode(playerdata[player.UserId]["Quests"])
playerdata[player.UserId]["Quests"] = encoded2

Converting what I saved back to a table:

local decoded1 = game.HttpService:JSONDecode(playerdata[player.UserId]["Quests"])
playerdata[player.UserId]["Quests"] = decoded1

Try: JSON stringify

This can help you a bit.

Can you explain a bit more in depth as to why that’d help me and how to use it?

This is super odd. It seems like it’s trying to escape an already escaped string. Could you be JSONEncoding the same thing (and then its result) multiple times, by any chance?

4 Likes

Example:

var obj = { name: "SingleSided", money: 30 };
var stringJSON = JSON.stringify(obj);
print(stringJSON )

The only problem is this is not lua but node.js / javascript

Why would you suggest something that isn’t a feature of the language…? JSONEncode is how Roblox’s Lua encodes JSON, so it should work the same.

2 Likes

Its should i use HttpService:JSONEncode and HttpService:JSONDecode for DataStore mostly.

What does your actual data look like?

3 Likes

Seems like I was somehow doing this, seems to be working now. Can’t believe I didn’t think about that. Thanks

1 Like

Getting a UTF-8 error when storing the table might also be a problem, perhaps you’re storing something that can’t be stored, like userdata (Vector3, CFrame ect.) or objects like parts. JSONening would prevent the error, but when decoding the objects you might encounter problems.

1 Like

Using JSON with DataStores, unless you are applying compression strategies that require the JSON format to turn a table into a string, is completely unnecessary. This work is already done internally because to be able to save data, the key and value need to be coerced into a string format.

It’s much more straight forward if you leave this work to the backend so that you can just raw-fetch and save tables back and forth through a DataStore instead of having to also involve encoding. It’s a weird gateway that just isn’t necessary to go through.

1 Like

This is 100% true, but I had already created a system that I didn’t plan on saving. Rather than redoing the entire thing, I decided to save it using JSON. Rewriting the entire thing for me would take hours, though you have a very good point.

local decoded1 = game.HttpService:JSONDecode(playerdata[player.UserId]["Quests"])
playerdata[player.UserId]["Quests"] = decoded1

I’m not even sure how this is effective even… You are setting playerdata[player.UserId]["Quests"] to itself…

This isn’t really relevant to the problem but I can explain what’s going on here.

The decoded1 variable is a reference to the value returned from JSONDecode with the value of the index playerdata[num][“Quests”] passed as an argument. This is just creating a reference to a value. The line below sets the value at the aforementioned index to this returned value.

It’s not setting it to itself. For example, it’d be like doing this:

local a = 1
local b

b = a
a = 2

print(a) -- 2
print(b) -- 1

Ohhh, I get it. Sometimes you have to do that to have a fixed instance of a value :stuck_out_tongue_winking_eye: