How to use the json to lua converter
- Paste your JSON text into the JSON input box.
- Set the number of Indent spaces you prefer (use 0 for compact output).
- The Lua table code appears immediately below as you type or paste.
- Copy the generated Lua code into your script or configuration file.
How JSON maps to Lua tables
Lua uses a single data structure called a table to represent both objects and arrays. When you convert json to lua, the mapping follows a few straightforward rules:
- JSON objects become Lua tables with string keys.
- JSON arrays become Lua tables with sequential numeric indices.
- The JSON
nullvalue becomes Lua'snil. - Booleans and numbers translate directly.
- Strings are wrapped in double quotes, with internal quotes and backslashes escaped.
For example, the JSON {"health": 100, "active": true} becomes the Lua table { active = true, health = 100 }. Notice that the keys are sorted alphabetically to ensure the output is always identical for the same input.
Valid Lua identifiers vs bracket syntax
In Lua, table keys that are valid identifiers can be written without quotes. A valid identifier starts with a letter or underscore and contains only letters, digits and underscores. This converter checks each key: if it matches the pattern ^[a-zA-Z_][a-zA-Z0-9_]*$ and is not a reserved word like and, end or function, it outputs key = value. Otherwise, it falls back to bracket notation: ["my-key"] = value.
This distinction matters because bracket syntax works for any string, including those with spaces, hyphens or Unicode characters, while bare identifiers are easier to read and more idiomatic in Lua scripts.
Handling null, empty structures and edge cases
JSON has null, but Lua uses nil. Assigning nil to a table field effectively removes it, which can cause unexpected behaviour if you iterate over the table later. If your application needs to distinguish between a missing key and an explicitly null value, consider replacing nil with a sentinel value after conversion.
Empty JSON objects ({}) and empty arrays ([]) both produce empty Lua tables. Because Lua does not natively distinguish between an empty object and an empty array, downstream code must rely on context or metadata to tell them apart.
Numbers are written with JavaScript's shortest round-trip representation, so no digits of the input are lost. Control characters inside strings are written as three-digit decimal escapes.
Reference: common conversions
| JSON | Lua | Notes |
|---|---|---|
{"a":1} | { a = 1 } | Object with one key |
[1,2,3] | { 1, 2, 3 } | Array becomes sequential table |
null | nil | Null maps to nil |
true | true | Boolean unchanged |
"line\nbreak" | "line\nbreak" | Escape sequences preserved |
{"my-key":1} | { ["my-key"] = 1 } | Hyphen requires brackets |
Worked examples
| Example | Input | Result |
|---|---|---|
| Simple object | JSON input: {"name":"Lua","version":5.4,"stable":true}; Indent spaces: 2 | { / name = "Lua", / stable = true, / version = 5.4, / } |
| Nested array | JSON input: {"items":[1,"two",null,{"nested":true}]}; Indent spaces: 2 | { / items = { / 1, / "two", / nil, / { / nested = true, / }, / }, / } |
| Unicode text | JSON input: {"greeting":"こんにちは ","emoji":"🚀"}; Indent spaces: 2 | { / emoji = "🚀", / greeting = "こんにちは ", / } |
FAQ
Does this json to lua converter upload my data?
No. The parsing and conversion happen entirely in your browser using JavaScript. Nothing is sent to any server.
How are JSON null values handled?
JSON null is converted to Lua nil. Keep in mind that assigning nil to a table key in Lua effectively deletes that key.
Why are the keys sorted alphabetically?
Sorting keys ensures deterministic output. Given the same JSON input, you will always get the exact same Lua string, which is useful for testing and version control.
Can I convert a JSON array to a Lua table?
Yes. JSON arrays are converted to Lua tables with sequential numeric indices, omitting explicit index keys just like standard Lua array syntax.
What happens if my JSON is invalid?
The tool displays the exact error message returned by the JSON parser, including the position where parsing failed, so you can fix the input quickly.
Are Unicode characters supported?
Yes. Unicode strings pass through unchanged. Lua 5.3 and later handle UTF-8 strings natively, so emoji and non-Latin scripts work correctly.
What indent size should I use?
Two or four spaces are standard for readability. Set the indent to 0 if you want compact, single-line output for embedding in minified scripts.