moz-sql-parser

raw JSON →
4.40.21126 verified Fri May 01 auth: no python

Moz SQL Parser is a Python library to parse SQL queries and extract the parse tree as a JSON-like structure. Currently at version 4.40.21126, it is actively maintained and does not have a fixed release cadence.

pip install moz-sql-parser
error ParseException: Expected {SelectStatement}
cause The SQL string contains syntax not supported by moz-sql-parser (e.g., certain dialect-specific syntax like MS SQL Server brackets).
fix
Simplify the SQL or check the parser's supported syntax. The parser does not understand all SQL dialects.
error AttributeError: module 'moz_sql_parser' has no attribute 'parse'
cause Installed an older version (<4.0.0) or imported incorrectly.
fix
Upgrade moz-sql-parser: pip install --upgrade moz-sql-parser. Use correct import: from moz_sql_parser import parse
gotcha The parsed output is a nested dictionary with keys like 'select', 'from', 'where', etc. The structure may differ from expectations for complex SQL (e.g., JOINs, subqueries). Always inspect the output.
fix Print or debug the output to understand the AST shape.
deprecated The function 'parse_sql' has been renamed to 'parse'. 'parse_sql' still works but may be removed in future versions.
fix Use 'from moz_sql_parser import parse' instead of 'parse_sql'.

Parse a simple SQL query and print the AST.

from moz_sql_parser import parse
sql = "SELECT a, b FROM table WHERE c > 1"
result = parse(sql)
print(result)