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.

4 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

Hello! Thanks for the report—we are looking into this. We’ll keep you posted on the progress and reach out if we need more details.

1 Like

We’ve looked into this and won’t be able to prioritize a fix for this particular issue.

JSONEncode covers most use cases, but we recognize that there are exceptions that might stem from differences in how Luau and JSON represent data. In cases which require a particular schema when encoding data, we encourage performing a custom encoding that suits your use case.

While we understand this may be disappointing, we truly appreciate the time you took to submit this report. If you have other concerns or questions, please reach out.

1 Like