https://github.com/eemeli/json-estree-ast
ESTree-compatible JSON AST parser
https://github.com/eemeli/json-estree-ast
Last synced: 12 months ago
JSON representation
ESTree-compatible JSON AST parser
- Host: GitHub
- URL: https://github.com/eemeli/json-estree-ast
- Owner: eemeli
- License: mit
- Created: 2018-03-16T12:47:17.000Z (about 8 years ago)
- Default Branch: master
- Last Pushed: 2021-01-29T16:13:36.000Z (over 5 years ago)
- Last Synced: 2025-06-19T00:03:59.711Z (12 months ago)
- Language: JavaScript
- Homepage: https://www.npmjs.com/package/json-estree-ast
- Size: 4.88 KB
- Stars: 2
- Watchers: 2
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# ESTree-compatible JSON AST parser
A small wrapper around [json-to-ast] that translates its output to the [ESTree Spec], which makes it usable for example as a [jscodeshift] parser.
[json-to-ast]: https://www.npmjs.com/package/json-to-ast
[ESTree Spec]: https://github.com/estree/estree
[jscodeshift]: https://www.npmjs.com/package/jscodeshift
## Installation
```
npm install json-estree-ast
```
## Usage
```js
const { parse } = require('json-estree-ast');
const options = {
loc: true, // include source location information. Default `true`
source: 'data.json' // include source information. Default `null`
};
parse('{"a": 1}', options);
```
Output
```js
{
type: 'Program',
body: [
{
type: 'ObjectExpression',
properties: [
{
type: 'Property',
key: {
type: 'Identifier',
name: 'a',
raw: '"a"',
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 4 },
source: 'data.json'
}
},
kind: 'init',
value: {
type: 'Literal',
value: 1,
raw: '1',
loc: {
start: { line: 1, column: 6 },
end: { line: 1, column: 7 },
source: 'data.json'
}
},
loc: {
start: { line: 1, column: 1 },
end: { line: 1, column: 7 },
source: 'data.json'
}
}
],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 8 },
source: 'data.json'
}
}
],
loc: {
start: { line: 1, column: 0 },
end: { line: 1, column: 8 },
source: 'data.json'
}
}
```
## Node types
#### ObjectExpression
```js
{
type: 'ObjectExpression',
properties: Property[],
loc?: SourceLocation
}
```
#### Property
```js
{
type: 'Property',
key: Identifier,
kind: 'init',
value: ObjectExpression | ArrayExpression | Literal,
loc?: SourceLocation
}
```
#### Identifier
```js
{
type: 'Identifier',
name: string,
raw: string,
loc?: SourceLocation
}
```
#### ArrayExpression
```js
{
type: 'ArrayExpression',
children: (ObjectExpression | ArrayExpression | Literal)[],
loc?: SourceLocation
}
```
#### Literal
```js
{
type: 'Literal',
value: string | number | boolean | null,
raw: string,
loc?: SourceLocation
}
```
#### SourceLocation
```js
{
source?: string,
start: SourcePosition,
end: SourcePosition
}
```
#### SourcePosition
```js
{
line: number,
column: number
}
```
## License
MIT