-
Notifications
You must be signed in to change notification settings - Fork 0
API JSON
Keith McGahey edited this page Jul 26, 2026
·
1 revision
JSON encoding and decoding. JSON null maps to a sentinel value, not nil — a nil table value would delete the key.
Encode a Lua value as a JSON string.
-
Parameters:
-
value(string | number | boolean | table): Value to encode. Usepicocalc.json.nullto emit JSONnull.
-
- Returns: (string) JSON text
local text = picocalc.json.encode({ name = "pico", score = 42 })
-- '{"name":"pico","score":42}'Decode a JSON string into Lua values. JSON null decodes to the picocalc.json.null sentinel (userdata), not nil.
-
Parameters:
-
text(string): JSON text
-
- Returns: (any) Decoded value
local data = picocalc.json.decode('{"name":"pico","score":42}')
picocalc.sys.log(data.name) -- "pico"Check whether a value is JSON null, without referencing the sentinel directly.
-
Parameters:
-
v(any): Value to test
-
-
Returns: (boolean)
trueifvis the JSONnullsentinel
if picocalc.json.isNull(data.middle_name) then
-- field is explicitly null
endSentinel value representing JSON null. Encode it to emit null; decoded null fields compare equal to it.
local text = picocalc.json.encode({ value = picocalc.json.null })
-- '{"value":null}'Decoding an API response and checking for null fields.
local body = '{"name":"pico","avatar_url":null,"score":1200}'
local user = picocalc.json.decode(body)
if picocalc.json.isNull(user.avatar_url) then
picocalc.sys.log(user.name .. " has no avatar")
else
picocalc.sys.log("avatar: " .. user.avatar_url)
end