This page documents @lanexio/parser-grammar-json, the JSON grammar package supporting strict JSON (RFC 8259), JSONC (comments and trailing commas), and JSON5 (ES5 relaxations).
Version: Stable
Module name: parser-grammar-json
Package: @lanexio/parser-grammar-json
Import path: @lanexio/parser-grammar-json
Layer: 2 (Grammar)
Runtime: Universal
Module format: ESM
Stability: Stable
Primary use case: Parse JSON, JSONC, and JSON5 documents into a flat AST.
Layer 2 dependency rules
This package depends only on Layer 1 core packages. It does not import from query, bridge, devtools, or entry packages. Grammar packs never depend on other grammar packs.
You need to parse strict JSON (RFC 8259) into a traversable AST.
You need to parse JSONC (JSON with Comments) or JSON5 (ES5 syntax relaxations).
You need structured AST with object members, arrays, and value nodes.
Boundary Description Inputs Uint8Array (source bytes) + optional ParseJsonDialectOptionsOutputs LexTree (flat AST rooted at JsonKind.Document)Side effects None Determinism Yes (same bytes + same dialect produce identical tree) External dependencies @lanexio/parser-coreNever-throw guarantee Yes Security surface None (parse only, no output generation)
Install the package.
pnpm add @lanexio/parser-grammar-json
npm install @lanexio/parser-grammar-json
yarn add @lanexio/parser-grammar-json
Import the named export.
import { parseJson } from ' @lanexio/parser-grammar-json ' ;
Requirement Required Layer Notes @lanexio/parser-coreYes 1 ^1.0.0
import { parseJson } from ' @lanexio/parser-grammar-json ' ;
const encoder = new TextEncoder ();
const bytes = encoder . encode ( ' {"key": [1, true, null]} ' );
const tree = parseJson (bytes);
console . log (tree . nodeCount );
console . log (tree . root . kind ); // JsonKind.Document
import { parseJsonc } from ' @lanexio/parser-grammar-json ' ;
const encoder = new TextEncoder ();
const tree = parseJsonc (encoder . encode ( ' /* comment */ { "key": 1 } ' ));
import { parseJson5 } from ' @lanexio/parser-grammar-json ' ;
const encoder = new TextEncoder ();
const tree = parseJson5 (encoder . encode ( ' { key: "value", } ' )); // unquoted keys, trailing comma
Export Type Description parseJson(bytes: Uint8Array) => LexTreeParse strict JSON (RFC 8259). Convenience wrapper. parseJsonc(bytes: Uint8Array) => LexTreeParse JSONC (JSON with Comments and trailing commas). parseJson5(bytes: Uint8Array) => LexTreeParse JSON5 (ES5 relaxations). parseWithDialect(bytes: Uint8Array, options: ParseJsonDialectOptions) => LexTreeParse with explicit dialect selection. STRICT_JSONJsonDialectStrict JSON dialect (RFC 8259). JSONCJsonDialectJSONC dialect (comments, trailing commas). JSON5JsonDialectJSON5 dialect (unquoted keys, single quotes, etc.). JsonKindconst objectNumeric kind IDs for all JSON node types. JsonFieldconst objectNumeric field IDs for JSON value slots. JSON_FIELD_NAMES_BY_IDreadonly string[]Field name lookup by numeric field ID. JSON_KIND_NAMES_BY_IDreadonly Record<number, string>Kind-name lookup by numeric ID. jsonGrammarLanexioParserPureGrammarGrammar descriptor for strict JSON. jsoncGrammarLanexioParserPureGrammarGrammar descriptor for JSONC. json5GrammarLanexioParserPureGrammarGrammar descriptor for JSON5. jsonRegistrationGrammarRegistrationRegistration for the unified grammar registry. jsoncRegistrationGrammarRegistrationRegistration for JSONC. json5RegistrationGrammarRegistrationRegistration for JSON5. jsonReuseOracleReuseOracleIncremental reuse oracle for strict JSON. jsoncReuseOracleReuseOracleIncremental reuse oracle for JSONC. json5ReuseOracleReuseOracleIncremental reuse oracle for JSON5.
Field Type Required Default Description dialectJsonDialectYes - The JSON dialect to use (STRICT_JSON, JSONC, or JSON5).
Property Type Description rootLexNodeRoot node (JsonKind.Document). nodeCountnumberTotal nodes in the tree. sourceUint8ArrayOriginal parsed bytes.
Type Purpose Notes ParseJsonDialectOptionsOptions for parseWithDialect { dialect: JsonDialect }JsonDialectJSON dialect definition Defines parsing rules JsonKindTypeUnion of all JsonKind values Type-safe kind reference JsonFieldTypeUnion of all JsonField values Type-safe field reference
Use parseWithDialect to select the parsing dialect explicitly. The convenience functions parseJson, parseJsonc, and parseJson5 are thin wrappers around parseWithDialect.
No direct accessibility surface. The JSON parser produces flat AST data structures. Consuming code is responsible for rendering output with appropriate semantics.
Concern Status Generated output semantics Not applicable (data format) ARIA attributes in serialized output Not applicable Semantic element round-trip Not applicable
parseJson, parseJsonc, and parseJson5 never throw on any byte sequence.
Threat Mitigation Status Malformed input byte sequence Panic-free guarantee: all inputs accepted, errors produce JsonKind.Error AST nodes Implemented
Package Relationship Layer Notes @lanexio/parser-coreRequires 1 Provides LexTree, LexNode, LexCursor @lanexio/parserConsumes 6 Unified entry point
Version Date Status Notable changes 1.0.02026-05-29Current Initial stable release. Apache-2.0.
Parsing JSON Guide: parse JSON with options and kind constants. Flat AST How the 16-byte node layout works.