
Security News
/Research
Wallet-Draining npm Package Impersonates Nodemailer to Hijack Crypto Transactions
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
tree-sitter-json
Advanced tools
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.
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');
}
jsonlint is a JSON parser and validator with a CLI and API. It provides similar functionality for parsing and validating JSON, but it does not generate a syntax tree like tree-sitter-json. Instead, it focuses on ensuring the JSON is well-formed and provides error messages for invalid JSON.
json5 is a JSON parser that allows for more relaxed JSON syntax, such as comments and trailing commas. While it also parses JSON, it is designed to handle a superset of JSON syntax, making it more flexible but less strict compared to tree-sitter-json.
fast-json-parse is a fast JSON parser that focuses on performance. It provides basic parsing functionality without generating a syntax tree. It is useful for applications where speed is critical and the additional features of tree-sitter-json are not required.
FAQs
JSON grammar for tree-sitter
The npm package tree-sitter-json receives a total of 322,014 weekly downloads. As such, tree-sitter-json popularity was classified as popular.
We found that tree-sitter-json demonstrated a healthy version release cadence and project activity because the last version was released less than a year ago. It has 7 open source maintainers collaborating on the project.
Did you know?
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.
Security News
/Research
Malicious npm package impersonates Nodemailer and drains wallets by hijacking crypto transactions across multiple blockchains.
Security News
This episode explores the hard problem of reachability analysis, from static analysis limits to handling dynamic languages and massive dependency trees.
Security News
/Research
Malicious Nx npm versions stole secrets and wallet info using AI CLI tools; Socket’s AI scanner detected the supply chain attack and flagged the malware.