util.TabeToJSON bug

So I convert 3 tables to JSON’s, 2 of them work correctly while one of them is returned as ‘0’
I printed the table and it all came out correctly so it is valid, but it doesnt convert correctly



Description	=	sssss
Gender	=	Female
Model	=	1
Name	=	sss

It is plainly converted to ‘0’ and baffles me as the other tables seem to work just fine with a slightly different structure.


Pretty sure it’s because of how you write your JSON string to the database, not a problem with the function.

I literally just do


The SQL field ( column ) type must not be a string.

Why are you even storing JSON in a database?

Because I’m saving a table set on a player

[editline]24th July 2015[/editline]

The column type is TEXT, that an issue? The other 2 are too but they seem to work.

What sort of table? Does it have nested tables in it? If not - you can do that without using JSON.

Actually, it doesn’t have nested tables but the other 2 do however, I’m guessing this is the issue. The structure of the table is at the top of the page.

Just do


And see what it prints. You will have an answer of whether it is a problem with the function or not.

it prints


Ignore the extra values, I added them. Tried without the added values, same result. Hmm, well I’m lost as to what the issue is.
Heres the full code

--player is defined as: local player = FindMetaTable("Player")
function player:SaveStats()
local result = sql.Query("UPDATE pw_char SET char_vars="..sql.SQLStr(util.TableToJSON(self.CharVars)).." AND char_inv="..sql.SQLStr(util.TableToJSON(self.Inventory)).." AND char_att="..sql.SQLStr(util.TableToJSON(self.Attributes)).." AND char_quests="..sql.SQLStr(util.TableToJSON(self.Quests)).." WHERE steam_id="..sql.SQLStr( self:SteamID() ).." AND char_id="..sql.SQLStr( self:GetCharID() ).."")

In general saving large json strings in a database is a bad idea, you should really look into the concept of database normalization.

As far as your code now, try replacing AND with commas (,) for each of the columns you’re settings (before the WHERE statement). At best it will fix your problem, at worst you’ll be following best practices.

Yeah that was my issue. I’ll look into database normalization, thank you :slight_smile: