JsonEncode
A value as JSON text.
The rules, the same on the server as in a client script - a value written by one is read back by the other:
- a table whose keys are exactly
1ton(nat least 1) is an array; an empty table is[]; any other table is an object, its number keys written as texts and its keys in order - a key that is neither a text nor a number is an error; nilis left out of a table, andJsonEncode(nil)isnull;- a number is written as the shortest text that reads back the same, an integer without a decimal point;
NaNand the infinities are an error; - texts stay UTF-8 (a text that is not valid UTF-8 is an error), with
",\and the control characters escaped and/left alone; - a table nested more than 32 deep, one that holds itself, and a function or any other value that is not data are errors; the text is at most 1 MB.
An error is not raised: the function returns nil and the reason. It works in every game, with or without the web layer.
Syntax
Section titled “Syntax”JsonEncode(value)| Parameter | Type | |
|---|---|---|
value |
any | a table, a text, a number, a boolean or nil (written as null) |
Returns
Section titled “Returns”string | nil, string - the JSON text, or nil and the reason
Example
Section titled “Example”local text = JsonEncode({ id = 7, items = { "sword", "bread" }, note = {} })print(text) --> {"id":7,"items":["sword","bread"],"note":[]}See also
Section titled “See also”JsonDecode · SendWebMessage · the Web interface group of the index
