2.1.0 • Published 7 years ago

json-to-ast v2.1.0

Weekly downloads
267,842
License
MIT
Repository
github
Last release
7 years ago

JSON AST parser

NPM NPM downloads Requirements Travis-CI

Install

> npm install json-to-ast

Usage

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

{
  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:

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

Property:

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

Identifier:

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

Array:

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

Literal:

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

AST explorer

Try it online on astexplorer.net

License

MIT

0.0.1

7 years ago

2.1.0

7 years ago

2.0.7

7 years ago

2.0.6

7 years ago

2.0.5

7 years ago

2.0.4

7 years ago

2.0.3

8 years ago

2.0.2

8 years ago

2.0.1

8 years ago

2.0.0

8 years ago

2.0.0-alpha1.3

8 years ago

2.0.0-alpha1.2

9 years ago

2.0.0-alpha1.1

9 years ago

2.0.0-alpha1

9 years ago

1.2.15

9 years ago

1.2.14

9 years ago

1.2.13

9 years ago

1.2.12

9 years ago

1.2.11

10 years ago

1.2.10

10 years ago

1.2.8

10 years ago

1.2.4

10 years ago

1.2.1

10 years ago

1.2.0

10 years ago

1.1.0

10 years ago

1.0.290

10 years ago

1.0.281

10 years ago

1.0.280

10 years ago

1.0.270

10 years ago

1.0.260

10 years ago

1.0.255

10 years ago

1.0.250

10 years ago

1.0.247

10 years ago

1.0.246

10 years ago

1.0.245

10 years ago

1.0.244

10 years ago

1.0.243

10 years ago

1.0.242

10 years ago

1.0.241

10 years ago

1.0.24

10 years ago

1.0.23

10 years ago

1.0.22

10 years ago

1.0.2

10 years ago

1.0.1

10 years ago

1.0.0

10 years ago