JSON grammar for Tree-sitter

0.24.8 · active · verified Sun Apr 12

tree-sitter-json provides the JSON language grammar for the `tree-sitter` parsing library. It enables parsing JSON source code into a concrete syntax tree, allowing for efficient structural analysis, manipulation, and syntax highlighting. The library is currently at version 0.24.8 and typically releases new versions in sync with the upstream `tree-sitter` core, which has a relatively active release cadence.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to load the `tree-sitter-json` grammar, initialize a parser, and parse a basic JSON string. It also shows a simple traversal of the resulting syntax tree to access the root node's type and its direct children for a JSON object. Input code must be `bytes`.

import tree_sitter_json
from tree_sitter import Language, Parser

# Load the JSON language grammar
JSON_LANGUAGE = Language(tree_sitter_json.language())

# Create a parser and set its language
parser = Parser()
parser.set_language(JSON_LANGUAGE)

# Example JSON source code (must be bytes)
json_code = b'''
{
    "name": "Alice",
    "age": 30,
    "isStudent": false,
    "courses": ["Math", "Science"]
}
'''

# Parse the code
tree = parser.parse(json_code)

# Get the root node and print its type and text
root_node = tree.root_node
print(f"Root node type: {root_node.type}")
print(f"Root node text: {root_node.text.decode('utf8')}")

# Example: Iterate through top-level object pairs (requires understanding of JSON grammar nodes)
if root_node.type == 'object':
    for child in root_node.children:
        if child.type == 'pair':
            key_node = child.child_by_field_name('key')
            value_node = child.child_by_field_name('value')
            if key_node and value_node:
                print(f"  Key: {key_node.text.decode('utf8')}, Value: {value_node.text.decode('utf8')}")

view raw JSON →