Registry / serialization / json-to-ast

json-to-ast

JSON →
library2.1.0jsnpmunverified

A lightweight, zero-dependency JSON parser that produces an Abstract Syntax Tree (AST) with full location information. Version 2.1.0 supports Emoji, runs on Node >=4, and provides error messages with precise character offsets. Unlike JSON.parse, it returns a structured tree with node types like 'object', 'array', 'identifier', 'literal' and detailed 'loc' (location) data including line, column, offset, and source. Ideal for linters, formatters, code mods, and tools needing to analyze or manipulate JSON structure. Last released in 2019 with no recent updates, possibly in maintenance mode.

npm install json-to-ast
INSTALL
IMPORT
SIG · JSON-TO-AST
J
json-to-ast
serializationjavascriptv2.1.0
Install
—
Import
—
Disk
—
Pass rate
0/ 6
Env Coverage0 / 6
glibc
18–22
musl
18–22
Install & Compatibility
Where this runs
tested against v? · npm install
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
node 18–226 runs
build_error
glibc
node 18–226 runs
build_error
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

default
✓ import parse from 'json-to-ast'
✗ const parse = require('json-to-ast')
ESM-only since v2? Actually CJS is still supported but default export is used; both works, but ensure you handle default import correctly.
parse
✓ import { parse } from 'json-to-ast'
✗ const { parse } = require('json-to-ast')
Named export 'parse' also available; both default and named export point to same function.
type imports
✓ import type { ASTNode, ValueNode } from 'json-to-ast'
✗ import { ASTNode } from 'json-to-ast'
Type imports require 'import type' in TypeScript if using as types only.

Parses a JSON string into an AST with location info (loc: true) and source name.

import parse from 'json-to-ast'; const json = '{ "name": "Alice", "age": 30 }'; const ast = parse(json, { loc: true, source: 'example.json' }); console.log(JSON.stringify(ast, null, 2)); // Output: // { // "type": "object", // "children": [ // { // "type": "property", // "key": { "type": "identifier", "value": "name", ... }, // "value": { "type": "literal", "value": "Alice", ... } // }, // { // "type": "property", // "key": { "type": "identifier", "value": "age", ... }, // "value": { "type": "literal", "value": 30, ... } // } // ], // "loc": { ... } // }
Debug
Known issues
breakingv2.0.0 renamed 'verbose' option to 'loc' and changed node type names (e.g., 'value' to 'literal', 'key' to 'identifier').
fix
Use 'loc: true' instead of 'verbose: true'; update node type references to new names.
affects: <2.0.0
breakingv2.0.0 changed 'rawValue' to 'raw' in literal nodes.
fix
Access 'raw' instead of 'rawValue' on literal nodes.
affects: <2.0.0
breakingv2.0.0 changed 'position' property to 'loc'.
fix
Access 'loc' instead of 'position' for location data.
affects: <2.0.0
deprecatedThe package has not been updated since 2019. No future updates expected.
fix
Consider alternatives like @humanwhocodes/momoa or moo/ace for active development.
affects: >=2.1.0
gotchaDefault export and named export 'parse' both exist, but some bundlers may treat default export differently.
fix
Use named import for clarity: import { parse } from 'json-to-ast';
affects: >=2.0.0
Errors
Common errors & fixes
Error: The 'source' option must be a string
Passing a non-string value (e.g., number or object) for the 'source' option.
fix
Ensure 'source' is a string: parse(json, { source: 'file.json' })
TypeError: parse is not a function
Importing the module incorrectly in CJS or ESM environment.
fix
Use correct import: import parse from 'json-to-ast' or const parse = require('json-to-ast').default (for CJS with named export).
SyntaxError: Unexpected token in JSON at position X
The input string is not valid JSON or has extra characters. json-to-ast throws a custom error with line/column info.
fix
Check the JSON input validity; the error message includes location details (line, column, offset).
Upgrade
Version history
2.1.0latest on npm
Audit
Dependencies

No dependency data recorded yet.

Agent activity
12 hits · last 30 days
node
12
Resources
json-to-ast — npm install json-to-ast · libregistry