# json-to-ast

> JSON AST parser

Latest version **2.1.0** (published 2018-12-23) · MIT license · 0 weekly downloads

## Install

```sh
npm install json-to-ast
pnpm add json-to-ast
yarn add json-to-ast
bun add json-to-ast
```

## Health

**Score 23/100 (F)** — status: abandoned.

Positive: has types package; no vulnerabilities; high quality score.

Warnings: low downloads; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.0 |
| Published | 2018-12-23 |
| First published | 2016-05-19 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | separate (@types/json-to-ast) |
| Module format | CommonJS |
| Node | >= 4 |
| Dependencies | 2 |
| Unpacked size | 142.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 251 |
| Author | Vlad Trushin |
| Maintainers | vtrushin |
| Keywords | json-parser, parser, ast, json, tree |

## Links

- npm: https://www.npmjs.com/package/json-to-ast
- Repository: https://github.com/vtrushin/json-to-ast
- Issues: https://github.com/vtrushin/json-to-ast/issues
- npm.io page: https://npm.io/package/json-to-ast

## Dependencies (2)

- [grapheme-splitter](https://npm.io/package/grapheme-splitter.md) ^1.0.4
- [code-error-fragment](https://npm.io/package/code-error-fragment.md) 0.0.230

## Alternatives

- [@mapbox/jsonlint-lines-primitives](https://npm.io/package/@mapbox/jsonlint-lines-primitives.md) — 5.3M weekly downloads
- [reftools](https://npm.io/package/reftools.md) — 3.5M weekly downloads
- [@hey-api/openapi-ts](https://npm.io/package/@hey-api/openapi-ts.md) — 3.5M weekly downloads
- [@mapbox/geojson-rewind](https://npm.io/package/@mapbox/geojson-rewind.md) — 2.4M weekly downloads
- [turbo-stream](https://npm.io/package/turbo-stream.md) — 1.7M weekly downloads

## Recent versions

- 2.1.0 (latest) — 2018-12-23
- 2.0.7 — 2018-12-04
- 2.0.6 — 2018-12-04
- 2.0.5 — 2018-10-09
- 2.0.4 — 2018-10-09
- 2.0.3 — 2018-05-04
- 2.0.2 — 2017-10-25
- 2.0.1 — 2017-10-25
- 2.0.0 — 2017-10-25
- 2.0.0-alpha1.3 — 2017-09-26
- 2.0.0-alpha1.2 — 2017-04-02
- 2.0.0-alpha1.1 — 2017-04-02
- 2.0.0-alpha1 — 2017-04-02
- 1.2.15 — 2017-02-02
- 1.2.14 — 2017-02-02
- … 29 more at https://npm.io/package/json-to-ast/versions

## README

[npm-icon]:           https://img.shields.io/npm/v/json-to-ast.svg
[npm-downloads-icon]: https://img.shields.io/npm/dm/json-to-ast.svg
[npm-url]:            https://www.npmjs.com/package/json-to-ast

[node-versions-icon]: https://img.shields.io/node/v/json-to-ast.svg
[node-url]:           https://nodejs.org

[test-icon]:          https://travis-ci.org/vtrushin/json-to-ast.svg?branch=master
[test-url]:           https://travis-ci.org/vtrushin/json-to-ast

[coverage-icon]:      https://coveralls.io/repos/github/vtrushin/json-to-ast/badge.svg?branch=master
[coverage-url]:       https://coveralls.io/github/vtrushin/json-to-ast?branch=master

[astexplorer-url]:   https://astexplorer.net/#/gist/6e328cf76a27ca85e552c9cb583cdd74/1077c8842337972509a29bc9063d17bf90a1a492

# JSON AST parser

[![NPM][npm-icon]][npm-url]
[![NPM downloads][npm-downloads-icon]][npm-url]
[![Requirements][node-versions-icon]][node-url]
[![Travis-CI][test-icon]][test-url]

## Install
```
> npm install json-to-ast
```

## Usage

```js
const parse = require('json-to-ast');

const settings = {
  // Appends location information. Default is <true>
  loc: true,
  // Appends source information to node’s location. Default is <null>
  source: 'data.json'
};

parse('{"a": 1}', settings);
```

Output
```js
{
  type: 'Object',
  children: [
    {
      type: 'Property',
      key: {
        type: 'Identifier',
        value: 'a',
        raw: '"a"',
        loc: {
          start: { line: 1, column: 2, offset: 1 },
          end: { line: 1, column: 5, offset: 4 },
          source: 'data.json'
        }
      },
      value: {
        type: 'Literal',
        value: 1,
        raw: '1',
        loc: {
          start: { line: 1, column: 7, offset: 6 },
          end: { line: 1, column: 8, offset: 7 },
          source: 'data.json'
        }
      },
      loc: {
        start: { line: 1, column: 2, offset: 1 },
        end: { line: 1, column: 8, offset: 7 },
        source: 'data.json'
      }
    }
  ],
  loc: {
    start: { line: 1, column: 1, offset: 0 },
    end: { line: 1, column: 9, offset: 8 },
    source: 'data.json'
  }
}
```

## Node types

Object:
```js
{
  type: 'Object',
  children: <Property>[],
  loc?: Object
}
```

Property:
```js
{
  type: 'Property',
  key: <Identifier>,
  value: Object | Array | <Literal>,
  loc?: Object
}
```

Identifier:
```js
{
  type: 'Identifier',
  value: string,
  raw: string,
  loc?: Object
}
```

Array:
```js
{
  type: 'Array',
  children: (Object | Array | <Literal>)[],
  loc?: Object
}
```

Literal:
```js
{
  type: 'Literal',
  value: string | number | boolean | null,
  raw: string,
  loc?: Object
}
```

## AST explorer
[Try it online][astexplorer-url] on [astexplorer.net](https://astexplorer.net/)

## License
MIT

---
_Source: https://npm.io/package/json-to-ast · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
