Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

tree-sitter-json

Package Overview
Dependencies
Maintainers
7
Versions
23
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

tree-sitter-json

JSON grammar for tree-sitter

  • 0.24.8
  • latest
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
475K
increased by3.44%
Maintainers
7
Weekly downloads
 
Created

What is tree-sitter-json?

The tree-sitter-json npm package provides a parser for JSON using the Tree-sitter parsing library. It allows you to parse JSON into a syntax tree, which can be useful for syntax highlighting, code analysis, and other tasks that require understanding the structure of JSON data.

What are tree-sitter-json's main functionalities?

Parsing JSON

This feature allows you to parse a JSON string into a syntax tree. The code sample demonstrates how to set up the parser with the JSON language and parse a simple JSON string.

const Parser = require('tree-sitter');
const JSON = require('tree-sitter-json');

const parser = new Parser();
parser.setLanguage(JSON);

const sourceCode = '{"key": "value"}';
const tree = parser.parse(sourceCode);

console.log(tree.rootNode.toString());

Navigating the Syntax Tree

This feature allows you to navigate the syntax tree generated from the JSON string. The code sample shows how to access the root node and its first child, and print their types and string representations.

const Parser = require('tree-sitter');
const JSON = require('tree-sitter-json');

const parser = new Parser();
parser.setLanguage(JSON);

const sourceCode = '{"key": "value"}';
const tree = parser.parse(sourceCode);

const rootNode = tree.rootNode;
const firstChild = rootNode.firstChild;

console.log(firstChild.type); // object
console.log(firstChild.toString());

Error Handling

This feature allows you to detect syntax errors in the JSON string. The code sample demonstrates how to check if the parsed syntax tree contains any errors.

const Parser = require('tree-sitter');
const JSON = require('tree-sitter-json');

const parser = new Parser();
parser.setLanguage(JSON);

const sourceCode = '{"key": "value"'; // Missing closing brace
const tree = parser.parse(sourceCode);

if (tree.rootNode.hasError()) {
  console.log('Syntax error detected');
} else {
  console.log('No syntax errors');
}

Other packages similar to tree-sitter-json

Keywords

FAQs

Package last updated on 11 Nov 2024

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

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc