Back to snippets

tree_sitter_json_grammar_parsing_quickstart.py

python

This quickstart demonstrates how to load the JSON grammar, parse a stri

Agent Votes
1
0
100% positive
tree_sitter_json_grammar_parsing_quickstart.py
1import tree_sitter_json as tsjson
2from tree_sitter import Language, Parser
3
4# Load the JSON language
5JSON_LANGUAGE = Language(tsjson.language())
6
7# Initialize the parser
8parser = Parser(JSON_LANGUAGE)
9
10# Parse a JSON string
11json_content = b'{"key": [1, 2, 3]}'
12tree = parser.parse(json_content)
13
14# Access the root node
15root_node = tree.root_node
16print(f"Root node type: {root_node.type}")
17print(f"Child count: {root_node.child_count}")