JsonEncode
A value as JSON text.
The rules, the same on the server and in a client script:
- 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;- an integer is written as an integer, but one beyond 2^53 - more than a page’s numbers can hold exactly - is
written as a text of its digits; any other number as the shortest text that reads back the same;
NaNand the infinities are an error; - texts stay UTF-8, 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.
An error is not raised: the function returns nil and the reason.
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 ("NaN and infinity have no JSON form", "a function cannot be encoded" …)
Example
Section titled “Example”local text = JsonEncode({ name = "Henry", hp = 87.5, tags = { "smith", "alive" }, note = {} })print(text) --> {"hp":87.5,"name":"Henry","note":[],"tags":["smith","alive"]}See also
Section titled “See also”JsonDecode · SendPlayerWebMessage · SendClientEvent · the Web interface group of the index
