Skip to content

JsonEncode

A value as JSON text.

The rules, the same on the server and in a client script:

  • a table whose keys are exactly 1 to n (n at 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;
  • nil is left out of a table, and JsonEncode(nil) is null;
  • 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; NaN and 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.

JsonEncode(value)
Parameter Type
value any a table, a text, a number, a boolean or nil (written as null)

string | nil, string - the JSON text, or nil and the reason ("NaN and infinity have no JSON form", "a function cannot be encoded" …)

local text = JsonEncode({ name = "Henry", hp = 87.5, tags = { "smith", "alive" }, note = {} })
print(text) --> {"hp":87.5,"name":"Henry","note":[],"tags":["smith","alive"]}

JsonDecode · SendPlayerWebMessage · SendClientEvent · the Web interface group of the index