Garry's Mod Wiki

Garry's Mod Wiki

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

Description

Converts a table to a JSON string. Keep in mind that not every data type can be stored in the JSON format, notably any entity will not be written, as if it wasn't in the table. Same goes for materials and textures, etc.

See util.JSONToTable for the opposite function.

All keys are strings in the JSON format, so all keys of other types will be converted to strings!
This can lead to loss of data where a number key could be converted into an already existing string key! (for example in a table like this: {["5"] = "ok", [5] = "BBB"})

Arguments

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

Returns

1 string
A JSON formatted string containing the serialized data

Example

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

This site is a community mirror of the official Garry's Mod wiki.. This site is not maintained by Facepunch Studios.

Page content is automatically updated four times a day. Edits and history are not available.

Last Parsed: Loading...