Garry's Mod Wiki


  string util.TableToJSON( table table, boolean prettyPrint = false )


Converts a table to a JSON string.

Trying to serialize or deserialize SteamID64s in JSON will NOT work correctly. They will be interpreted as numbers which cannot be precisely stored by JavaScript, Lua and JSON, leading to loss of data. You may want to use util.SteamIDFrom64 to work around this. Alternatively, just append a character to the SteamID64 to force util.JSONToTable to treat it as a string.
All keys are strings in the JSON format, so all keys will be converted to strings!
This will produce invalid JSON if the provided table contains nan or inf values.

Issue Tracker: 3561


1 table table
Table to convert.
2 boolean prettyPrint = false
Format and indent the JSON.


1 string
A JSON formatted string containing the serialized data


Writes the positions and angles of every player to a JSON file called playerstuff.json.

local Players = {} for k, v in ipairs(player.GetAll()) do Players[k] = { pos = v:GetPos(), ang = v:GetAngles() } end local tab = util.TableToJSON( Players ) -- Convert the player table to JSON file.CreateDir( "jsontest" ) -- Create the directory file.Write( "jsontest/playerstuff.json", tab) -- Write to .json