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
189 Stars 31 Forks MIT License 217 Commits 2 Opened issues

Description

JSON AST parser

Services available

!
?

Need anything else?

Contributors list

# 184,230
JavaScr...
jscodes...
Babel
HTML
185 commits
# 25,489
css-min...
React
Sublime...
Visual ...
11 commits
# 37,556
OCaml
Shell
12-fact...
Heroku
2 commits

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.