Skip to content

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 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;
  • a number is written as the shortest text that reads back the same, an integer without a decimal point; NaN and 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.

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

local text = JsonEncode({ id = 7, items = { "sword", "bread" }, note = {} })
print(text) --> {"id":7,"items":["sword","bread"],"note":[]}

JsonDecode · SendWebMessage · the Web interface group of the index