JSONEncode provides no apparent way to create empty JSON dictionaries

When attempting to encode a Lua dictionary into a JSON string to send as a request body with HttpService, I was unable to figure out a proper way in which I could pass an empty dictionary ({}) within the body (when using JSONEncode).

As stated within the documentation for JSONEncode, it automatically converts empty Lua tables to empty JSON arrays, which does make sense, however due to this, there is no way to properly pass an empty dictionary, AFAIK.

I am unsure if this post should be a feature request (considering this is expected behaviour) or a bug report (due to it preventing functionality), however it would be greatly appreciated if there could be some more insight on this as I am sure I am not the only developer who has encountered problems with this.

2 Likes

roblox treats an empty table as an Array, you can confirm this with the code below. theres afaik no way to make an “empty dictionary” because it sees them as an array instead

local TweenService = game:GetService("TweenService")

local function getType(value)
    local dummy = Instance.new("Frame")

    local success, ret = pcall(TweenService.Create, TweenService, dummy, TweenInfo.new(0), {
        AnchorPoint = value
    })

    dummy:Destroy()

    if success then
        return "Vector2"
    end

    return string.match(ret, "%(property is a 'Vector2', but given type is '(.+)'%)") or "none"
end

local dict = {a = "hi"}

print(getType(123)) --> double
print(getType(dict)) --> Dictionary -> JsonEncode -> {"a":"hi"}
dict.a = nil
warn(getType(dict)) --> Array -> JsonEncode -> []

Thanks for the report! I filed a ticket in our internal database.

1 Like