Need help with json-to-ast?
Click the “chat” button below for chat support from the developer who created it, or find similar developers for support.

About the developer

vtrushin
152 Stars 28 Forks MIT License 217 Commits 3 Opened issues

Description

JSON AST parser

Services available

!
?

Need anything else?

Contributors list

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 loc: true, // Appends source information to node’s location. Default is 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: [],
  loc?: Object
}

Property:

js
{
  type: 'Property',
  key: ,
  value: Object | Array | ,
  loc?: Object
}

Identifier:

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

Array:

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

Literal:

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

AST explorer

Try it online on astexplorer.net

License

MIT

We use cookies. If you continue to browse the site, you agree to the use of cookies. For more information on our use of cookies please see our Privacy Policy.