# json-ast

> JSON parser AST utilities

Latest version **2.1.7** (published 2017-04-11) · MIT license · 0 weekly downloads

## Install

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

## Health

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

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 2.1.7 |
| Published | 2017-04-11 |
| First published | 2016-07-25 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 0 |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 21 |
| Author | Romain Gaucher |
| Maintainers | neuroo |
| Keywords | json, extended-json, parser, ast |

## Links

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

## 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.7 (latest) — 2017-04-11
- 2.1.6 — 2016-09-12
- 2.1.5 — 2016-07-29
- 2.1.4 — 2016-07-29
- 2.1.3 — 2016-07-29
- 2.1.2 — 2016-07-28
- 2.1.1 — 2016-07-28
- 2.1.0 — 2016-07-27
- 2.0.1 — 2016-07-26
- 2.0.0 — 2016-07-25

## README

# A tolerant JSON parser

[![Build Status](https://travis-ci.org/neuroo/json-ast.svg?branch=master)](https://travis-ci.org/neuroo/json-ast)

## Features
The original code was developed by Vlad Trushin. Breaking modifications were made by [Romain Gaucher](https://twitter.com/rgaucher) to create a less strict JSON parser. Additionally, a more typical interaction with the AST has been implemented.

Current modifications and features as of `2.1.6` include:
* Creation of a `JsonDocument` root node and [more formal AST structure](./src/ast.js)
* Support for [inline comments](./test/cases/comment-in-object.json)
* Support for [multi-line comments](./test/cases/multi-line-comments-in-object.js)
* Support for [trailing commas and many consecutive commas](./test/cases/object-trailing-commas.json)
* Include visitor pattern to visit the AST
* Include a limited error-recovery mode trying to catch (when `junker` set to `true`):
  * [unclosed objects](./test/cases/object-unclosed-junker.json) or arrays
  * [too many closing braces](./test/cases/redundant-symbols-junker.json) or brackets
  * [automatic comma injection](./test/cases/asi-junker.json)
  * support for [unquoted keys](./test/cases/unquoted-keys-junker.json)
* [Conversion to a native JavaScript object](./test/index.js#L172) from `JsonNode`

[Basic examples](./examples/) are available to show how to use this package.

## JSONish
The JSON parser accepts a superset of the JSON language:
```json
// some comment
{
  "key1": "value1", // some other comments
  "key2": "value2",
  ,
  ,
  /*
    Oh dear! It's important to put this here.
    And we love commas too!
    And we're missing the closing brace...
  */
```

## Install
```shell
npm install json-ast
```

## Structure of the AST
As of 2.1.0, the AST is defined with the following types:

```javascript
[JsonNode] // Essentially an abstract class
  position: [Position]

[JsonDocument] extends [JsonNode]
  child: [?]*
  comments: [JsonComment]*

[JsonValue] extends [JsonNode]
  value: [?]

[JsonObject] extends [JsonNode]
  properties: [JsonProperty]*
  comments: [JsonComment]*

[JsonProperty] extends [JsonNode]
  key: [JsonKey]
  value: [?]*

[JsonKey] extends [JsonValue]

[JsonArray]
  items: *
  comments: [JsonComment]*

[JsonComment] extends [JsonValue]
[JsonString] extends [JsonValue]
[JsonNumber] extends [JsonValue]
[JsonTrue] extends [JsonValue]
[JsonFalse] extends [JsonValue]
[JsonNumber] extends [JsonValue]
```

All the types exists in [src/ast.js](src/ast.js).

## API
```javascript
import {parse, Visitor, AST} from 'json-ast';

// The visitor can stop at any time by assigning `Visitor.stop = true`
class MyVisitor extends Visitor {
  constructor() {
    super();
    this.comments = [];
  }

  comment(commentNode) {
    this.comments.push(commentNode.value);
  }
};

const JSON_BUFFER = `// Some comment
{
  "key": "value"
`;

// `verbose` will include the position in each node
const ast = parse(JSON_BUFFER, {verbose: true, junker: true});
assert(ast instanceof AST.JsonDocument);

const visitor = new MyVisitor();
ast.visit(visitor);
assert.deepEqual(visitor.comments, [" Some comment"]);

// One can also the `JsonNode.toJSON` static method to convert to a JavaScript object
const obj = JsonNode.toJSON(ast);
assert(obj.key === 'value');
```

### Parsing Options
The second argument of the `parse` function takes an object with the following settings:
* `verbose`: include positions in each AST node, `true` by default
* `junker`: enables an error recovery mode, `false` by default

## License
MIT Vlad Trushin and Romain Gaucher

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