# JSON5 in Lua and Neovim: Libraries and Usage | JSON5.dev


Source: https://json5.dev/lua/

---

# JSON5 for Lua and Neovim: Parsers, Install and Gotchas

Read JSON5 from Lua, pick a parser you can trust, and know what Neovim already does for you.

Lua has no standard JSON5 library. Two small open-source projects fill the gap: a single pure Lua file, and a Rust binding that you compile. Neither has a tagged release, and one of them carries an open bug that makes hexadecimal numbers panic.

This guide is written for Neovim plugin authors. It reads both repositories, runs the pure Lua one on 15 inputs, and covers the built-in `vim.json.decode`, null values, the missing array and object split, and Lua numbers.

![A crescent moon holding a JSON5 config card, with a code editor window glowing beside it](https://json5.dev/img/hero/lua.png)

**TL;DR:** If you only need comments, Neovim 0.12 and later documents `vim.json.decode(text, { skip_comments = true })`. For real JSON5 use `json5.lua` (one pure Lua file, decode only; read the gotchas first) or `lua-json5` (a Rust build step, `parse` only). Both are untagged, and the Rust one currently panics on hex numbers. Safest for a plugin: convert JSON5 to JSON at build time and decode JSON at run time.

[Libraries](#libraries) [Install](#install) [Basic Usage](#usage) [Reading Files](#files) [vim.json](#neovim-json) [Null, Arrays, Numbers](#data-model) [Test Results](#conformance) [Alternatives](#alternatives) [Gotchas](#gotchas) [FAQ](#faq) [Sources](#references)

![List icon](https://json5.dev/images/icons/list.png)

## Which Library, and How Mature Is It?

The [implementations](https://json5.dev/implementations/) page lists two Lua rows. I opened both repositories on 12-01-2026 and read the source and the GitHub API. Neither has a tag or a GitHub release, so there is no version number to pin: pin a commit instead.

`json5.lua`

`lua-json5`

**Repository**

MikuAuahDark/json5.lua

Joakker/lua-json5

**What it is**

Pure Lua, one file (`json5.lua`), no dependencies

A Rust library (`mlua` and `pest`) compiled to a shared library, loaded as a Lua module

**Runs on**

Described as Lua 5.1 or later; see the gotchas for what failed on Lua 5.4.2

LuaJIT by default; a Lua 5.4 build is a cargo feature

**License**

MIT

MIT (a separate `GRAMMAR_LICENSE` file sits beside it)

**Last commit**

11-26-2024

02-05-2026

**Tags and releases**

None

None (`Cargo.toml` says 0.1.0)

**Install**

Copy the file into your project

A build step that needs `cargo`

**On LuaRocks.org**

Not found

Not found

**Reads JSON5**

`json5.decode(text, opts)`

`require('json5').parse(text)`

**Writes JSON5**

No (the README says decoding only)

No (the module exports only `parse`)

**Open issues I found**

None (the issue list was empty)

Hex numbers panic (issue 14, 11-21-2026; fix proposed in pull request 15, still open)

"Not found on LuaRocks.org" means a search for `json5` on 12-01-2026 returned one module, `tree-sitter-json5`, which is a syntax-highlighting grammar and not a decoder. Both libraries load as a module named `json5`, so they collide if you put both on the same `package.path`.

Which should you pick? Both are small, single-author projects with no release process. Treat either as a vendored dependency you are willing to read and patch. The wider picture, with other languages, is on the [implementations](https://json5.dev/implementations/) page.

![Download icon](https://json5.dev/images/icons/download.png)

## Install

### json5.lua: vendor the file

The README's whole installation section is "drop `json5.lua` into your project". For a plugin, put it under your own namespace so it cannot collide with another plugin's copy, and keep the MIT notice at the top of the file.

`myplugin.nvim/ lua/ myplugin/ json5.lua -- copied from MikuAuahDark/json5.lua, pin the commit in a comment init.lua`

`local json5 = require('myplugin.json5')`

### lua-json5: a build step with lazy.nvim

The README shows these specs. Every machine that installs it needs a Rust toolchain with `cargo` on the `PATH`, because the module is compiled on the user's computer.

`-- lazy.nvim, Linux and macOS { 'Joakker/lua-json5', build = './install.sh', } -- Windows: the README's packer example has run = 'powershell ./install.ps1' -- commented in; the lazy.nvim equivalent would be that command as build (not tested)`

I read the scripts: `install.sh` runs `cargo build --features luajit --release`, then moves the result to `lua/json5.so` on Linux, or copies a `json5.dylib` and a `json5.so` on macOS. `install.ps1` moves `lua_json5.dll` to `lua\json5.dll`. The README adds that on macOS you may need `;?.dylib` on `package.cpath`. For plain Lua 5.4 it gives `cargo build --no-default-features --features lua54 --release`.

I did not build it: there is no Rust toolchain on the machine that tested this page. Closed issues in the repository (4, 5, 9) describe a module-not-found error on macOS and two crashes, so expect to debug the first install on an unusual setup. For a plugin you publish, requiring every user to compile Rust is a large ask; see the [alternatives](#alternatives).

![Code icon](https://json5.dev/images/icons/code.png)

## Basic Usage

**What was run:** the `json5.lua` results below were produced on 12-01-2026 with Lua 5.4.2 (LuaBinaries, Win64) against commit c59dbf6, with one change to the test harness described under [gotchas](#gotchas). The `lua-json5` statements come from its source and README: I could not build or run it.

### json5.lua

`local json5 = require('myplugin.json5') local text = [[ { // service settings name: 'billing', port: 0x1F90, ratio: .75, retries: +3, tags: ['eu', 'prod',], } ]] local cfg = json5.decode(text) -- cfg.name --> "billing" -- cfg.port --> 8080 (integer on Lua 5.4) -- cfg.ratio --> 0.75 -- cfg.retries --> 3 -- #cfg.tags --> 2 -- keep nulls: the second argument is a table of options local keep = json5.decode('{ a: null }', { null = json5.null })`

`json5.decode` is the only function, plus the `json5.null` sentinel. Errors are raised with `error()`, so wrap calls in `pcall`. The messages read like `expected comma got "b" at line 1 col 10`, prefixed with the file and line inside `json5.lua`.

### lua-json5

`local parse = require('json5').parse local ok, cfg = pcall(parse, [[ { /* comment */ ecma_identifier: 'works like a charm', "string keys": [1, 2, 3], // trailing comma below } ]])`

The sample is the README's own, wrapped in `pcall`. The module exports one function, `parse`. A syntax error comes back as a Lua error that wraps the Rust parser's error value (`src/parser.rs`), so catch it the same way.

![Document icon](https://json5.dev/images/icons/document.png)

## Reading a Config File in a Plugin

Neither library reads files, so read the file yourself, parse it once, and report the path when it fails. This loader uses only plain Lua plus the documented `vim.fn.stdpath`, and passes `vim.NIL` as the null sentinel so results match what `vim.json.decode` returns.

`local json5 = require('myplugin.json5') local function load_config(path) local f, err = io.open(path, 'rb') if not f then return nil, err end local text = f:read('*a') f:close() -- json5.lua cannot read a UTF-8 byte order mark (see gotchas) text = text:gsub('^\239\187\191', '') local ok, result = pcall(json5.decode, text, { null = vim.NIL }) if not ok then return nil, path .. ': ' .. result end return result end local path = vim.fn.stdpath('config') .. '/myplugin.json5' local cfg, err = load_config(path) if not cfg then vim.notify(err, vim.log.levels.ERROR) end`

Parse at startup or on first use, not on every keystroke: the pure Lua parser is slow on large input (numbers under [gotchas](#gotchas)). Validate the shape afterwards, since a parser only proves the syntax. The same idea in other languages is on the [Python](https://json5.dev/python/), [Ruby](https://json5.dev/ruby/) and [Dart](https://json5.dev/dart/) guides, and file editing in Vim is on the [Vim and Neovim tools](https://json5.dev/tools/vim/) page.

![Neovim logo](https://json5.dev/images/tech/neovim.svg)

## Neovim's Built-In vim.json.decode

`vim.json.decode(str, opts)` parses strict JSON. Its documented options are `luanil` and, newer, `skip_comments`. I read the reference page (`runtime/doc/lua.txt`) at several tags:

Option

What the documentation says

Documented in

`luanil`

`{ object?: boolean, array?: boolean }`: convert JSON `null` in objects or arrays to Lua `nil` instead of `vim.NIL`

v0.11.0 and v0.12.0 (present in both)

`skip_comments`

Boolean, default `false`: allows `//` line comments and block comments anywhere whitespace is valid; comments are treated as whitespace

v0.12.0 (absent from the v0.11.0 page)

`-- Neovim 0.12 or later, per the documentation local cfg = vim.json.decode(text, { skip_comments = true })`

The 0.12 news page lists the option under new features, and the source commit is titled "vim.json.decode() can allow comments" (merged 02-11-2026). The latest release I saw on 12-01-2026 was v0.12.7. I could not run Neovim here, so none of this was executed.

**That is JSON with comments, not JSON5.** The documentation lists no option for trailing commas, single-quoted strings, unquoted keys, hexadecimal numbers or `Infinity`. Do not assume any of them parse. This is the same family as the [JSONC comparison](https://json5.dev/compare/jsonc/) describes. On Neovim 0.11 and older there is no such option: strip comments first or use a library.

`vim.fn.json_decode` is the older Vimscript function. The lua-json5 README benchmarks against it and says that for speed you should stay with the built-in; the JSON5 features are the only reason to choose the library. For the comments question in general, see [comments in JSON](https://json5.dev/comments-in-json/).

![Structure icon](https://json5.dev/images/icons/structure.png)

## Null, Arrays and Numbers in Lua

Lua's data model is smaller than JSON's, so each library has to choose. The table lists the choices; the "tested" rows ran on Lua 5.4.2, the rest are from documentation or source. For the format's own types see [JSON5 data types](https://json5.dev/spec/data-types/).

Question

`vim.json.decode`

`json5.lua`

`lua-json5`

JSON `null`

`vim.NIL`; `nil` with `luanil`

`nil` by default; with `{ null = x }` your sentinel, or `json5.null`

`nil` (source, `val.rs`)

`[1, null, 3]`

Three items, with `vim.NIL` in the middle

Tested: a two-item table `{1, 3}`; the null vanished and 3 moved to index 2

A `nil` hole (source; not run)

Empty `{}`

`vim.empty_dict()`

Tested: a plain empty table

A plain empty table (source)

Empty `[]`

`{}`, an empty Lua table

Tested: a plain empty table, same as `{}`

A plain empty table (source)

Number type

Per Lua 5.1 semantics, one number type

Tested on 5.4: `1` is an integer, `1.0` and `1e2` are floats

Always a 64-bit float (source: `Value::Number(f64)`)

`Infinity`, `NaN`

Not documented; not in strict JSON

Tested: `math.huge`, `-math.huge`, a NaN

Grammar accepts them; Rust's float parser accepts both words

### One table type for arrays and objects

Lua has a single table type, so after decoding there is no flag that says "this was an array". Neovim's documentation states the rule it uses when converting tables back: a table with consecutive integer keys 1 to N is a list, a table with string keys is a dictionary, and an empty table is a list unless it came from `vim.empty_dict()`. `vim.json.decode` follows that convention in the other direction. `json5.lua` does not: an empty object and an empty array decode to identical tables, so a [round trip](https://json5.dev/glossary/#round-trip) through a table cannot tell them apart. If your plugin writes the data back out, keep a schema or use `vim.empty_dict()` where an object is required.

### Lua 5.4 integers, Lua 5.1 and LuaJIT floats

The Lua 5.4 manual says numbers have two subtypes, integer and float. The Lua 5.1 manual says there is one number type, a double-precision float. Neovim's documentation says Lua 5.1 is the permanent interface for plugins and that Neovim uses LuaJIT on supported platforms, so a plugin should expect every number to be a double. In my Lua 5.4.2 run, `json5.lua` returned `9007199254740993` as an exact integer; on a double it is not representable and would round. Keep IDs and large counters in strings, or treat anything above 2^53 as unsafe, which is also the JSON5 and JavaScript convention.

`Infinity` and `NaN` decode, but they will not survive `vim.json.encode` or any strict JSON writer, and `NaN` never equals itself in a comparison. Test with `x ~= x`.

![Test icon](https://json5.dev/images/icons/test.png)

## What json5.lua Actually Does on 15 Inputs

Tested on 12-01-2026: Lua 5.4.2 (Win64 build from the LuaBinaries project), `json5.lua` at commit c59dbf6 (11-26-2024), one real run of each input. This is 15 hand-picked cases, not the official `json5-tests` suite. See the wider [conformance](https://json5.dev/conformance/) page for that suite.

Input

Result

What came back

Line comment: `{ // hi` then `a: 1 }`

![Yes](https://json5.dev/images/icons/yes.png)

`{a=1}`

Block comment: `{ /* hi */ a: 1 }`

![Yes](https://json5.dev/images/icons/yes.png)

`{a=1}`

Trailing commas: `{ a: [1,2,], b: 2, }`

![Yes](https://json5.dev/images/icons/yes.png)

Both parsed

Single quotes: `{ a: 'x"y' }`

![Yes](https://json5.dev/images/icons/yes.png)

The string `x"y`

Unquoted keys: `{ $a_1: 1, b2: 2 }`

![Yes](https://json5.dev/images/icons/yes.png)

Both keys

Hex: `[0xFF, -0x10, +0x1]`

![Yes](https://json5.dev/images/icons/yes.png)

255, -16, 1 (integers)

`[Infinity, -Infinity, NaN]`

![Yes](https://json5.dev/images/icons/yes.png)

inf, -inf, nan (floats)

Multi-line string: backslash then newline inside `'...'`

![Yes](https://json5.dev/images/icons/yes.png)

The newline was removed

`[.5, 5., +1, -.5e1]`

![Yes](https://json5.dev/images/icons/yes.png)

0.5, 5.0, 1, -5.0

Strict JSON with `null` and `\u00e9`

![Yes](https://json5.dev/images/icons/yes.png)

Parsed; the `null` was dropped from the array

Hex escape: `['\x41']`

![No](https://json5.dev/images/icons/no.png)

Error: invalid hex escape sequence at line 1 col 5

UTF-8 byte order mark before `{a:1}`

![No](https://json5.dev/images/icons/no.png)

Error: invalid value literal at line 1 col 1

Octal-looking number: `[012]`

![Yes](https://json5.dev/images/icons/yes.png)

Rejected, as the spec requires

Missing comma: `{ a: 1 b: 2 }`

![Yes](https://json5.dev/images/icons/yes.png)

Rejected with a line and column

Empty key list: `{,}`

![No](https://json5.dev/images/icons/no.png)

Rejected, but with a raw Lua error: `attempt to compare nil with number`

Twelve of fifteen behave as the specification says. The three failures are real and each is cheap to avoid. Unicode escapes (`\u0042`), the null character escape and an escaped quote were accepted in a separate run, and duplicate keys silently keep the last value.

I did not run `lua-json5`. Reading its grammar (`src/json5.pest`): comments, [trailing commas](https://json5.dev/glossary/#trailing-comma), single quotes, unquoted keys including Unicode letters, [line continuations](https://json5.dev/glossary/#line-continuation), `.5`, `5.`, a [leading](https://json5.dev/glossary/#leading-decimal-point) plus, `Infinity`, `NaN` and hex escapes are all accepted by the grammar. The grammar also accepts hex numbers, but the code converts every number with Rust's float parser, whose documented grammar has no hexadecimal form: that is the open bug.

![Compare icon](https://json5.dev/images/icons/compare.png)

## Alternatives for Plugin Authors

### Convert at build time, ship JSON

If the file is yours (defaults, a bundled schema), write it in JSON5 and convert it with `npx json5 -s 2 -o defaults.json defaults.json5`. The plugin then calls `vim.json.decode` on plain JSON, with no parser dependency and the fastest path. You lose the comments in the shipped copy only. The [command-line guide](https://json5.dev/tools/cli/) has the flags.

### Comments only: skip\_comments

If users only need comments in a file they edit, Neovim 0.12 and later documents `skip_comments`. Require that version, and say so in your plugin's README. Test your own trailing-comma cases before promising anything more.

### Vendor json5.lua

Best when users hand-write real JSON5 and you cannot ship a compiler. Fix the hex escape line and strip the [byte order mark](https://json5.dev/glossary/#byte-order-mark) first, pin the commit, and add your own tests for the inputs you accept.

### lua-json5 for your own setup

Reasonable in a personal config where you control the machine and have `cargo`. Wait for issue 14 to be closed, or avoid hex numbers, before you depend on it. Not a good default for a published plugin.

A syntax-highlighting grammar is a different job: Tree-sitter highlighting for `.json5` files is covered on the [Vim and Neovim](https://json5.dev/tools/vim/) page, and does not parse values into Lua tables. To check a file without any Lua, use the [online validator](https://json5.dev/tools/online-validator/).

![Warning icon](https://json5.dev/images/icons/warning.png)

## Things That Will Surprise You

### 1\. json5.lua does not load on Lua 5.4

Despite "Lua 5.1 or later" in the README, `require` failed on Lua 5.4.2 with `attempt to call a nil value (global 'newproxy')`, at line 729, where `json5.null` is created. `newproxy` exists in Lua 5.1 and LuaJIT, so Neovim is fine. To run the tests above I defined a stand-in `newproxy` in the test script and left the library file unchanged.

### 2\. The hex escape is broken

Line 315 reads `text:sub(2, 3)`, which is `x4` for `\x41`, so every `\xHH` escape raises an error. Changing it to `text:sub(3, 4)` made `['\x41\x7a']` decode to `Az` in a copy I tested. The official spec allows this escape.

### 3\. Byte order marks fail

The whitespace list in `json5.lua` lacks U+FEFF, which the specification counts as whitespace. A file saved with a BOM by some Windows editors is rejected. Strip the three bytes before decoding, as the loader above does. More in [encoding problems](https://json5.dev/troubleshooting/encoding/).

### 4\. Nulls silently shorten arrays

With the default options, `[1, null, 3]` became a two-item table and a null object value disappeared. Pass `{ null = vim.NIL }` when position or presence matters.

### 5\. It is slow on big input

The source comments call its whitespace scan O(n^2). One run each on my machine: 56,786 bytes took 0.66 s and 115,786 bytes took 1.00 s. Fine for a config file, wrong for a data file.

### 6\. Error positions and error types

The reported column pointed two characters past the real position in my missing-comma sample, and `{,}` and `{a:1,,}` raised a raw comparison error instead of a syntax message. Show the line, not the column, and always use `pcall`.

### 7\. Hex panics in lua-json5

Issue 14 reports that `{a: 0x1F}` panics with a parse error and includes a `pcall` example. The proposed fix in pull request 15 was open when I read it. The results on this page cannot tell you whether a panic is catchable on your build.

### 8\. Object key order is not kept

Lua tables have no order, and `lua-json5` builds objects from a Rust hash map. Never rely on `pairs` order; sort keys when output must be stable. Duplicate keys keep the last value in both.

## JSON5 in Lua: Common Questions

### Does vim.json.decode read JSON5?

No. It reads JSON, and from Neovim 0.12 it can also skip comments through the documented `skip_comments` option. Single quotes, unquoted keys and hex are not documented.

### Which library should a published plugin use?

Neither as a hard dependency. Convert to JSON at build time, or vendor `json5.lua` with its bugs fixed. A required Rust build is a barrier for users.

### Can Lua write JSON5?

Neither library does. Write JSON with `vim.json.encode`, which is valid JSON5. Comments, unquoted keys and trailing commas cannot be produced that way.

### How do I keep JSON null in a Lua table?

Use `vim.NIL` (the default of `vim.json.decode`) or pass it as the `null` option of `json5.decode`. Plain `nil` removes the key.

### Is it safe to parse untrusted input?

Both build data and never run the input as code. Neither documents a size or depth limit, and the pure Lua parser is slow on large input, so cap the file size yourself. See the [security tutorial](https://json5.dev/tutorials/security/).

### Where can I see what each syntax error means?

The [error reference](https://json5.dev/errors/) covers the common mistakes in any language, and the [specification overview](https://json5.dev/spec/) lists the full grammar.

## See Also

[![Neovim logo](https://json5.dev/images/tech/neovim.svg)

### Vim and Neovim

Filetype, highlighting and the language server

](https://json5.dev/tools/vim/)[![List icon](https://json5.dev/images/icons/list.png)

### Implementations

Libraries across languages

](https://json5.dev/implementations/)[![Test icon](https://json5.dev/images/icons/test.png)

### Conformance

What the official tests check

](https://json5.dev/conformance/)

## Check a File Before Your Plugin Reads It

Paste a config into the validator to see whether it is valid JSON5, then convert it to JSON for `vim.json.decode`.

[Open the Validator](https://json5.dev/tools/online-validator/) [Convert to JSON](https://json5.dev/tools/online-converter/)

## Sources

(14)

1.  [MikuAuahDark/json5.lua: README, source and commit history (commit c59dbf6, 11-26-2024)](https://github.com/MikuAuahDark/json5.lua)
2.  [Joakker/lua-json5: README, Cargo.toml, install scripts, `src/` and commit history (02-05-2026)](https://github.com/Joakker/lua-json5)
3.  [lua-json5 issue 14: hexadecimal numbers panic with ParseFloatError](https://github.com/Joakker/lua-json5/issues/14)
4.  [lua-json5 pull request 15: read hexadecimal numbers instead of panicking](https://github.com/Joakker/lua-json5/pull/15)
5.  [Neovim v0.12.0 `runtime/doc/lua.txt`: vim.json.decode (`luanil`, `skip_comments`), vim.NIL, vim.empty\_dict and the table conversion rules](https://raw.githubusercontent.com/neovim/neovim/v0.12.0/runtime/doc/lua.txt)
6.  [Neovim v0.11.0 `runtime/doc/lua.txt`: the same page without `skip_comments`](https://raw.githubusercontent.com/neovim/neovim/v0.11.0/runtime/doc/lua.txt)
7.  [Neovim 0.12 news: the `skip_comments` option](https://raw.githubusercontent.com/neovim/neovim/master/runtime/doc/news-0.12.txt)
8.  [Neovim releases (v0.12.7 was the latest on 12-01-2026)](https://github.com/neovim/neovim/releases)
9.  [Lua 5.4 reference manual: integer and float subtypes](https://www.lua.org/manual/5.4/manual.html)
10.  [Lua 5.1 reference manual: a single number type](https://www.lua.org/manual/5.1/manual.html)
11.  [LuaBinaries on SourceForge: the Lua 5.4.2 Windows build used for the tests](https://sourceforge.net/projects/luabinaries/)
12.  [Rust documentation: f64 and its FromStr grammar](https://doc.rust-lang.org/std/primitive.f64.html)
13.  [LuaRocks.org search for json5 (one result, a syntax grammar)](https://luarocks.org/search?q=json5)
14.  [The JSON5 Data Interchange Format 1.0.0](https://spec.json5.org/)

Last reviewed: 12-01-2026.
