{"id":4813,"library":"tree-sitter-json","title":"JSON grammar for Tree-sitter","description":"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.","status":"active","version":"0.24.8","language":"en","source_language":"en","source_url":"https://github.com/tree-sitter/tree-sitter-json","tags":["parser","AST","JSON","grammar","tree-sitter","code analysis","syntax tree"],"install":[{"cmd":"pip install tree-sitter-json","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Provides the core Python bindings and parsing engine required to use the tree-sitter-json grammar.","package":"tree-sitter"}],"imports":[{"note":"The core tree-sitter Python bindings are imported from 'tree_sitter', not 'tree_sitter_json'.","symbol":"Language","correct":"from tree_sitter import Language, Parser"},{"note":"The `tree_sitter_json` package itself exposes the language factory function, not the `Language` class directly. The `Language` class comes from the `tree_sitter` package.","wrong":"from tree_sitter_json import Language","symbol":"tree_sitter_json.language","correct":"import tree_sitter_json\nJSON_LANGUAGE = Language(tree_sitter_json.language())"}],"quickstart":{"code":"import tree_sitter_json\nfrom tree_sitter import Language, Parser\n\n# Load the JSON language grammar\nJSON_LANGUAGE = Language(tree_sitter_json.language())\n\n# Create a parser and set its language\nparser = Parser()\nparser.set_language(JSON_LANGUAGE)\n\n# Example JSON source code (must be bytes)\njson_code = b'''\n{\n    \"name\": \"Alice\",\n    \"age\": 30,\n    \"isStudent\": false,\n    \"courses\": [\"Math\", \"Science\"]\n}\n'''\n\n# Parse the code\ntree = parser.parse(json_code)\n\n# Get the root node and print its type and text\nroot_node = tree.root_node\nprint(f\"Root node type: {root_node.type}\")\nprint(f\"Root node text: {root_node.text.decode('utf8')}\")\n\n# Example: Iterate through top-level object pairs (requires understanding of JSON grammar nodes)\nif root_node.type == 'object':\n    for child in root_node.children:\n        if child.type == 'pair':\n            key_node = child.child_by_field_name('key')\n            value_node = child.child_by_field_name('value')\n            if key_node and value_node:\n                print(f\"  Key: {key_node.text.decode('utf8')}, Value: {value_node.text.decode('utf8')}\")\n","lang":"python","description":"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`."},"warnings":[{"fix":"Always ensure that the `tree-sitter` Python package is kept up-to-date with `tree-sitter-json`. If issues arise, try reinstalling both packages: `pip install --upgrade tree-sitter tree-sitter-json`.","message":"The `tree-sitter` core library (which `tree-sitter-json` relies on) had an internal ABI bump to version 15 in v0.25.0 (February 2025). Ensure your installed `tree-sitter` Python package is compatible with the ABI version used to compile `tree-sitter-json`. Mismatched versions can lead to crashes or unexpected behavior.","severity":"breaking","affected_versions":"tree-sitter core >=0.25.0"},{"fix":"Encode your input string to bytes, typically using `my_string.encode('utf8')`, before passing it to `parser.parse()`.","message":"The `parser.parse()` method expects input as `bytes`, not a standard Python `str`. Providing a string will result in a `TypeError`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Ensure both `tree-sitter` and `tree-sitter-json` are installed: `pip install tree-sitter tree-sitter-json`.","message":"The `tree-sitter-json` package only provides the grammar. The actual parsing engine and Python bindings are provided by the `tree-sitter` package. Forgetting to install `tree-sitter` will prevent `tree-sitter-json` from being used.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-12T00:00:00.000Z","next_check":"2026-07-11T00:00:00.000Z"}