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

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.

List icon

Which Library, and How Mature Is It?

The 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
RepositoryMikuAuahDark/json5.luaJoakker/lua-json5
What it isPure Lua, one file (json5.lua), no dependenciesA Rust library (mlua and pest) compiled to a shared library, loaded as a Lua module
Runs onDescribed as Lua 5.1 or later; see the gotchas for what failed on Lua 5.4.2LuaJIT by default; a Lua 5.4 build is a cargo feature
LicenseMITMIT (a separate GRAMMAR_LICENSE file sits beside it)
Last commit11-26-202402-05-2026
Tags and releasesNoneNone (Cargo.toml says 0.1.0)
InstallCopy the file into your projectA build step that needs cargo
On LuaRocks.orgNot foundNot found
Reads JSON5json5.decode(text, opts)require('json5').parse(text)
Writes JSON5No (the README says decoding only)No (the module exports only parse)
Open issues I foundNone (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 page.

Download icon

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.

Code icon

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. 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

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). Validate the shape afterwards, since a parser only proves the syntax. The same idea in other languages is on the Python, Ruby and Dart guides, and file editing in Vim is on the Vim and Neovim tools page.

Neovim logo

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:

OptionWhat the documentation saysDocumented in
luanil{ object?: boolean, array?: boolean }: convert JSON null in objects or arrays to Lua nil instead of vim.NILv0.11.0 and v0.12.0 (present in both)
skip_commentsBoolean, default false: allows // line comments and block comments anywhere whitespace is valid; comments are treated as whitespacev0.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 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.

Structure icon

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.

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 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

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 page for that suite.

InputResultWhat came back
Line comment: { // hi then a: 1 }Yes{a=1}
Block comment: { /* hi */ a: 1 }Yes{a=1}
Trailing commas: { a: [1,2,], b: 2, }YesBoth parsed
Single quotes: { a: 'x"y' }YesThe string x"y
Unquoted keys: { $a_1: 1, b2: 2 }YesBoth keys
Hex: [0xFF, -0x10, +0x1]Yes255, -16, 1 (integers)
[Infinity, -Infinity, NaN]Yesinf, -inf, nan (floats)
Multi-line string: backslash then newline inside '...'YesThe newline was removed
[.5, 5., +1, -.5e1]Yes0.5, 5.0, 1, -5.0
Strict JSON with null and \u00e9YesParsed; the null was dropped from the array
Hex escape: ['\x41']NoError: invalid hex escape sequence at line 1 col 5
UTF-8 byte order mark before {a:1}NoError: invalid value literal at line 1 col 1
Octal-looking number: [012]YesRejected, as the spec requires
Missing comma: { a: 1 b: 2 }YesRejected with a line and column
Empty key list: {,}NoRejected, 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, single quotes, unquoted keys including Unicode letters, line continuations, .5, 5., a leading 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

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 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 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 page, and does not parse values into Lua tables. To check a file without any Lua, use the online validator.

Warning icon

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.

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.

Where can I see what each syntax error means?

The error reference covers the common mistakes in any language, and the specification overview lists the full grammar.

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.