Luadata by Example

Empty Tables

Lua has a single table type that serves as both arrays and objects. This creates an ambiguity when converting to JSON: an empty Lua table {} could be either a JSON array [] or a JSON object {}.

lua
inventory = {}
settings = {}

By default, luadata renders empty tables as null. This is a safe default since it doesn't assert a type that might be wrong.

output
{
  "inventory": null,
  "settings": null
}

But sometimes you know the intended type. Maybe inventory should be an empty array, or settings should be an empty object. The WithEmptyTableMode option lets you control this behavior.

Note that the chosen mode applies to all empty tables in the output. If you need per-field control, a JSON Schema can declare each field's type explicitly — see Schema vs Heuristics.

The next example shows all available modes.

Want more flexibility? Open the interactive converter to try any Lua input with all available options.