The WithEmptyTableMode option controls how empty Lua tables are rendered in JSON. There are four modes to choose from.
Given this input:
data = {}
EmptyTableNull (default) renders as null:
{
"data": null
}
EmptyTableOmit removes the key entirely:
{}
EmptyTableArray renders as an empty JSON array:
{
"data": []
}
EmptyTableObject renders as an empty JSON object:
{
"data": {}
}
package main
import (
"fmt"
"io"
"github.com/mmobeus/luadata"
)
func main() {
input := `data = {}`
reader, err := luadata.TextToJSON("input", input,
luadata.WithEmptyTableMode("array"),
)
if err != nil {
panic(err)
}
result, _ := io.ReadAll(reader)
fmt.Println(string(result))
}
{
"data": []
}
From the CLI, use the --empty-table flag:
echo 'data = {}' | luadata tojson --empty-table=object -
{
"data": {}
}
Want more flexibility? Open the interactive converter to try any Lua input with all available options.