Is there any way to create via a lua module, a storage record with a value that looks like this:
"{names:[]}"
It seems that from lua any empty table is always created as an empty object in json, and only becomes an json array when there’s more than one nested object.
that’s supposed to be a pair of square brackets, not a checkbox.
Lua doesn’t strictly differentiate between ‘objects’ and ‘arrays’, the table
type takes care of both so by default the JSON encoder can’t be sure if you intended to encode an empty table object or table array.
If you really need to strictly control it the easiest way is string substitution for the empty case on specific fields:
local json = "{\"names\":[],\"no_change\":[]}"
json = string.gsub(json, "\"names\":%[%]", "\"names\":{}")
print(json)
Will print:
{"names":{},"no_change":[]}
@oscargoldman This is one of the limitations with JSON and Lua. Lua only has one type to represent both arrays and map types. It uses an array-indexed table or a keyed table.
i.e.
local array = {"a", "b", "c"}
local map = {
{"key1", "a"},
{"key2", "b"}
}
Is it necessary for you to store empty arrays at all in your JSON structure?
it was part of initializing required records for a new user, I was running into some serialization issues when reading the records back but we can work around it.