Skip to content

JSON_EMPTY_ARRAY

JSON_EMPTY_ARRAY is a global sentinel value that serializes as [] in JSON output. Use it when you need an empty array instead of the default empty object {}.

Assign JSON_EMPTY_ARRAY to any table key to force an empty JSON array:

-- Without sentinel: empty table becomes {}
json_encode({ items = {} }) -- '{"items":{}}'
-- With sentinel: force empty array
json_encode({ items = JSON_EMPTY_ARRAY }) -- '{"items":[]}'
get.data = function(self)
return {
status = 200,
body = {
items = JSON_EMPTY_ARRAY,
meta = {} -- this stays as {}
},
}
end

The array parameter on json_encode works independently. JSON_EMPTY_ARRAY always produces [] regardless of the flag:

json_encode(JSON_EMPTY_ARRAY) -- "[]"
json_encode(JSON_EMPTY_ARRAY, false) -- "[]"
json_encode(JSON_EMPTY_ARRAY, true) -- "[]"