Socket
Book a DemoInstallSign in
Socket

json-estree-ast

Package Overview
Dependencies
Maintainers
1
Versions
2
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

json-estree-ast

ESTree-compatible JSON AST parser

latest
Source
npmnpm
Version
1.0.1
Version published
Weekly downloads
1.1K
20.98%
Maintainers
1
Weekly downloads
 
Created
Source

ESTree-compatible JSON AST parser

A small wrapper around json-to-ast that translates its output to the ESTree Spec, which makes it usable for example as a jscodeshift parser.

Installation

npm install json-estree-ast

Usage

const { parse } = require('json-estree-ast');

const options = {
  loc: true,  // include source location information. Default `true`
  source: 'data.json'  // include source information. Default `null`
};

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

Output

{
  type: 'Program',
  body: [
    {
      type: 'ObjectExpression',
      properties: [
        {
          type: 'Property',
          key: {
            type: 'Identifier',
            name: 'a',
            raw: '"a"',
            loc: {
              start: { line: 1, column: 1 },
              end: { line: 1, column: 4 },
              source: 'data.json'
            }
          },
          kind: 'init',
          value: {
            type: 'Literal',
            value: 1,
            raw: '1',
            loc: {
              start: { line: 1, column: 6 },
              end: { line: 1, column: 7 },
              source: 'data.json'
            }
          },
          loc: {
            start: { line: 1, column: 1 },
            end: { line: 1, column: 7 },
            source: 'data.json'
          }
        }
      ],
      loc: {
        start: { line: 1, column: 0 },
        end: { line: 1, column: 8 },
        source: 'data.json'
      }
    }
  ],
  loc: {
    start: { line: 1, column: 0 },
    end: { line: 1, column: 8 },
    source: 'data.json'
  }
}

Node types

ObjectExpression

{
  type: 'ObjectExpression',
  properties: Property[],
  loc?: SourceLocation
}

Property

{
  type: 'Property',
  key: Identifier,
  kind: 'init',
  value: ObjectExpression | ArrayExpression | Literal,
  loc?: SourceLocation
}

Identifier

{
  type: 'Identifier',
  name: string,
  raw: string,
  loc?: SourceLocation
}

ArrayExpression

{
  type: 'ArrayExpression',
  children: (ObjectExpression | ArrayExpression | Literal)[],
  loc?: SourceLocation
}

Literal

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

SourceLocation

{
  source?: string,
  start: SourcePosition,
  end: SourcePosition
}

SourcePosition

{
  line: number,
  column: number
}

License

MIT

FAQs

Package last updated on 29 Jan 2021

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts