JsonDecode
JSON text as a value.
The reverse of JsonEncode: an object becomes a table with text keys, an array a table with the keys 1 to n,
and a JSON null inside them leaves the place empty (in an array the other values keep their positions). The game’s
Lua keeps its numbers as 32-bit floats, so an integer beyond 16777216 (2^24) - an id, a large sum - cannot be held
exactly and comes back as a text of its digits; convert it with tonumber only if you know it is small enough, or keep it
as the text it is.
A null on its own gives nil, so a nil result alone does not say the text was wrong - look at the second value. Text
that is not valid JSON, is more than 32 levels deep or is over 1 MB gives nil and the reason. Nothing in the text can
run anything: what comes back is data. It works in every game, with or without the web layer.
Syntax
Section titled “Syntax”JsonDecode(text)| Parameter | Type | |
|---|---|---|
text |
string | one JSON value |
Returns
Section titled “Returns”any, string | nil - the value - or nil and the reason when the text could not be read
Example
Section titled “Example”local value, why = JsonDecode('{"id":7,"items":["sword","bread"]}')if value then print(value.items[2]) else print("bad JSON: " .. why) end --> breadSee also
Section titled “See also”JsonEncode · OnWebEvent · the Web interface group of the index
