Lua to JSON Table

Parses a Lua table literal, key = value pairs, ["key"]/[42] bracket keys, positional array entries, nested tables, and comments, into equivalent JSON using a hand-rolled recursive-descent parser, no external Lua runtime required. A free online tool from Staaarter, right in your browser.

Runs locallyUpdated 2026-07-26

Overview

Introduction

Lua table literals show up constantly outside of Lua itself: game mod configs, Neovim/LÖVE/Roblox settings, and data files for tools embedding Lua as a config language all use the same `{ key = value, ... }` syntax. This tool parses that syntax directly into JSON without needing a Lua interpreter installed anywhere.

It's a small, purpose-built recursive-descent parser for table literals specifically, not a general Lua-to-JSON runtime bridge, and it runs entirely in the browser.

What Is Lua to JSON Table?

A hand-rolled parser for Lua's table constructor syntax: `key = value` named fields, `["string key"]`/`[42]` bracket-notation fields, bare positional values (Lua's array part), nested tables, both Lua string quote styles with basic escape handling, numbers, `true`/`false`/`nil`, and both Lua comment styles.

It converts the parsed table into the closest equivalent JSON representation: a table with only positional entries becomes a JSON array, a table with only named/bracket entries becomes a JSON object, and a table mixing both (which Lua allows but JSON has no direct equivalent for) becomes a JSON object with the positional entries placed under their 1-based index as a string key alongside the named ones.

The parser is self-contained: it doesn't evaluate Lua code, call functions, or resolve variables, it only reads the literal structure of a table constructor exactly as written.

How Lua to JSON Table Works

Input is first tokenized: whitespace and both comment styles (`--` line comments and `--[[ ... ]]` block comments) are skipped, and the tokenizer recognizes string literals (with escape handling), numbers (including hex literals like `0x1F`), identifiers/keywords, and the punctuation a table literal needs (`{ } [ ] = , ; -`).

A recursive-descent parser then reads a table constructor: for each field it distinguishes a bracket key (`[expr] = value`), a named key (`identifier = value`, checked by looking one token ahead for `=`), or a bare positional value, recursing into `parseValue` for nested tables and all value types.

The resulting raw table structure (a list of named key/value pairs plus a list of positional values, preserving source order) is converted to JSON last: array if only positional, object if only named/bracket, and object with stringified numeric indices for the positional part if the table mixes both styles.

When To Use Lua to JSON Table

Use it when you need to work with data currently expressed as a Lua table, a mod/game config, a Neovim plugin setting, an exported save file, in a JSON-based tool, pipeline, or language that doesn't read Lua natively.

It's also useful for quickly inspecting or diffing Lua config data using JSON-aware tools (a JSON diff, a JSON schema generator, a JSON formatter) once it's been converted, rather than reading nested Lua table syntax by eye.

Features

Advantages

  • No external Lua interpreter or runtime dependency; parsing happens entirely with a small hand-written parser running in the browser.
  • Handles the full range of practical table syntax: named fields, bracket-notation keys (string and numeric), positional entries, nested tables, and mixed positional+keyed tables.
  • Strips both Lua comment styles correctly, without corrupting string literals that happen to contain a `--` sequence.
  • Reports a specific line/column location for a syntax error instead of an opaque failure.

Limitations

  • This is a table-literal parser, not a Lua interpreter: it can't evaluate expressions, resolve variables, call functions, or execute any other Lua language construct. The input must be a literal, self-contained `{ ... }` table.
  • Lua's long-bracket string syntax (`[[...]]`, `[==[...]==]`) isn't supported as a string value; only `"..."` and `'...'` quoted strings are.
  • An empty table `{}` is always converted to an empty JSON object, since Lua doesn't distinguish an empty array from an empty map; adjust by hand if you specifically need an empty array there.
  • Mixed positional+keyed tables are represented with stringified numeric indices for the positional part, a reasonable but not universal convention; other Lua-to-JSON tools may represent the same table differently.

Examples

Named fields, a nested table, and a bracket string key

Input

{ name = "Ada", age = 30, tags = {"admin", "editor"}, ["nested-key"] = { x = 1 } }

Output

{
  "name": "Ada",
  "age": 30,
  "tags": ["admin", "editor"],
  "nested-key": { "x": 1 }
}

Named and bracket keys produce a JSON object; the tags table has only positional entries, so it becomes a JSON array.

Positional-only table

Input

{"admin", "editor"}

Output

["admin", "editor"]

With no named keys at all, the table converts directly to a JSON array.

Mixed positional and named entries

Input

{ 10, 20, x = 1 }

Output

{ "1": 10, "2": 20, "x": 1 }

Lua allows mixing array-style and keyed entries in one table; since JSON has no equivalent, the positional entries become string-keyed "1"/"2" object properties alongside "x".

Best Practices & Notes

Best Practices

  • Paste a self-contained table literal starting with `{`, not a full Lua script or a `local x = { ... }` assignment; strip the surrounding assignment first if needed.
  • Double-check mixed positional+keyed tables after conversion. The 1-based stringified index convention this tool uses is reasonable but not the only way to represent that shape in JSON.
  • Review numeric bracket keys (like `[42]`) in the output; JSON's string-only object keys mean they'll appear as `"42"`, not the number 42.
  • For very large or deeply nested table literals, convert in sections if you hit readability limits rather than trying to review one enormous JSON blob at once.

Developer Notes

The tokenizer and parser are hand-written specifically for table-constructor syntax (Lua 5.4 manual section 3.4.9), not a general Lua grammar; it accepts a strict subset sufficient for real-world config-style tables. Escape handling in string literals covers the common set (\n, \t, \r, \\, \", \', \0, \a, \b, \f, \v) via a lookup table rather than Lua's full numeric/hex escape grammar. Numeric bracket keys are stringified with `String(value)` for use as JSON object keys, and the conversion step is a separate pass over the parsed raw table structure (positional list + named list, in source order) rather than being interleaved with parsing.

Lua to JSON Table Use Cases

  • Converting a game mod or plugin's Lua config table into JSON for use in a non-Lua tool or pipeline.
  • Migrating Lua-based configuration (LÖVE, Neovim, Roblox, OpenResty) to a JSON-based configuration format.
  • Inspecting or diffing Lua-authored data using JSON-aware tooling (schema generators, formatters, diff viewers).
  • Extracting structured data embedded in a Lua table literal for use in a script or application written in another language.

Common Mistakes

  • Pasting a full Lua script (with `local`, function calls, or multiple statements) instead of a single self-contained table literal; only the table constructor syntax itself is supported.
  • Expecting long-bracket Lua strings (`[[...]]`) to parse as string values; they aren't supported, only quoted strings are.
  • Assuming a mixed positional+keyed table's JSON representation (stringified numeric indices) is a universal standard; it's a reasonable convention, not the only one other tools use.

Tips

  • If parsing fails, check the reported line and column first; unmatched brackets and stray trailing commas after the last field are the most common causes.
  • Run the JSON output through the JSON Schema Generator afterward if you want to document the shape of a converted config.
  • For hex-encoded numeric values in Lua (like `0xFF`), check the JSON output uses the decimal equivalent, since JSON numbers have no hex literal syntax.

References

Frequently Asked Questions