This plugin allows the Jsonic JSON parser to parse JSONC format files (JSON with Comments).
JSONC is a strict superset of JSON that adds single-line (//) and
block (/* */) comments. Trailing commas in objects and arrays can be
optionally enabled.
![]() | This open source module is sponsored and supported by Voxgig. |
|---|
The documentation below is organized along the Diátaxis quadrants:
- Quick start — tutorial
- How-to guides — task recipes
- Reference — API surface
- JSONC format — explanation
Install:
npm install @jsonic/jsonc @jsonic/jsonic-nextParse:
import{Jsonic}from'@jsonic/jsonic-next'import{Jsonc}from'@jsonic/jsonc'constj=Jsonic.make().use(Jsonc)constresult=j('{ "name": "app", /* version */ "version": "1.0" }')// => { name: 'app', version: '1.0' }Install:
go get github.com/jsonicjs/jsonc/goParse:
package main
import (
"fmt"
jsonic "github.com/jsonicjs/jsonic/go"
jsonc "github.com/jsonicjs/jsonc/go"
)
funcmain() {
j:=jsonic.Make()
j.Use(jsonc.Jsonc)
result, err:=j.Parse(`{ "name": "app", /* version */ "version": "1.0" }`)
iferr!=nil {
panic(err)
}
fmt.Println(result)
// => map[name:app version:1.0]
}TypeScript:
constj=Jsonic.make().use(Jsonc,{allowTrailingComma: true})j('{ "debug": true, "verbose": false, }')// => { debug: true, verbose: false }Go:
j:=jsonic.Make()
j.Use(jsonc.Jsonc, map[string]any{"allowTrailingComma": true})
result, _:=j.Parse(`{ "debug": true, "verbose": false, }`)TypeScript:
constj=Jsonic.make().use(Jsonc,{disallowComments: true})j('{ "foo": /* not allowed */ true }')// throwsGo:
j:=jsonic.Make()
j.Use(jsonc.Jsonc, map[string]any{"disallowComments": true})TypeScript — parse errors throw:
try{j('{ "bad": }')}catch(err){console.error(err.message)}Go — errors are returned:
if_, err:=j.Parse(`{ "bad": }`); err!=nil {
fmt.Println(err)
}TypeScript:
import{readFileSync}from'node:fs'constj=Jsonic.make().use(Jsonc,{allowTrailingComma: true})constconfig=j(readFileSync('tsconfig.json','utf8'))Go:
src, _:=os.ReadFile("tsconfig.json")
j:=jsonic.Make()
j.Use(jsonc.Jsonc, map[string]any{"allowTrailingComma": true})
config, _:=j.Parse(string(src))functionJsonc(jsonic: Jsonic,options?: JsoncOptions): voidtypeJsoncOptions={allowTrailingComma?: boolean// default: falsedisallowComments?: boolean// default: false}Register with jsonic.use(Jsonc, options?). After registration, invoke
the jsonic instance as a function on a source string; it returns the
parsed value or throws on syntax errors.
| Option | Type | Default | Effect |
|---|---|---|---|
allowTrailingComma | boolean | false | Permit a trailing comma before } and ] |
disallowComments | boolean | false | Reject // and /* */ comments (strict JSON) |
funcJsonc(j*jsonic.Jsonic, pluginOptsmap[string]any) errorRegister with j.Use(jsonc.Jsonc) or j.Use(jsonc.Jsonc, opts) where
opts is a map[string]any. Parse then returns (any, error) —
map[string]any for objects, []any for arrays, float64 for numbers,
string, bool, or nil.
| Key | Type | Default | Effect |
|---|---|---|---|
allowTrailingComma | bool | false | Permit a trailing comma before } and ] |
disallowComments | bool | false | Reject // and /* */ comments (strict JSON) |
JSONC follows RFC 8259 (JSON) with these extensions:
- Line comments:
//to end of line - Block comments:
/* */(non-nesting) - Trailing commas: optional, in objects and arrays
All other JSON rules apply:
- Strings must be double-quoted
- Standard escapes only:
\"\\\/\b\f\n\r\t\uXXXX - Numbers: integer, decimal, scientific notation (no hex, octal, binary)
- Keywords:
true,false,null(case-sensitive) - Property names must be double-quoted strings
The plugin layers JSONC rules on top of jsonic, which is intentionally
lenient in some places vs. strict RFC 8259. The test suite runs the
nst/JSONTestSuite corpus in
strict mode (disallowComments: true) and pins the known-lenient
cases in test/jsontestsuite.test.ts (see N_KNOWN_LENIENT). Examples
of accepted-but-non-RFC input include numbers with leading zeros and
unquoted object keys. Use an RFC-strict parser if byte-perfect RFC 8259
rejection is required.
Conformance testing uses third-party corpora under MIT License:
- nst/JSONTestSuite by Nicolas
Seriot — vendored at
test/JSONTestSuite/(thetest_parsing/corpus). - microsoft/node-jsonc-parser —
parse-level test cases ported into
test/jsonc.test.ts.
See THIRD_PARTY_NOTICES.md for details.
MIT. Copyright (c) 2021-2025 Richard Rodger and contributors.
