{"id":9124,"library":"mo-sql-parsing","title":"More SQL Parsing!","description":"mo-sql-parsing is a Python library designed to parse SQL queries into a JSON-izable parse tree. It aims to convert various SQL dialects, initially targeting MySQL, into a structured JSON format, making it easier to translate or analyze SQL for different datastores. As of version 11.697.25301, the library is actively maintained, with ongoing issue resolution and regular updates, as indicated by its high version number and recent activity on GitHub (October 2024 Project Status).","status":"active","version":"11.697.25301","language":"en","source_language":"en","source_url":"https://github.com/klahnakoski/mo-sql-parsing","tags":["sql","parsing","json","ast","query-language"],"install":[{"cmd":"pip install mo-sql-parsing","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Core utility dependency","package":"mo-dots"},{"reason":"Core utility dependency","package":"mo-future"},{"reason":"Forked pyparsing for faster parsing, fundamental for SQL grammar definition","package":"mo-parsing"},{"reason":"Core utility dependency","package":"mo-imports"}],"imports":[{"note":"The 'moz_sql_parser' library is deprecated; 'mo-sql-parsing' is an actively maintained fork.","wrong":"from moz_sql_parser import parse","symbol":"parse","correct":"from mo_sql_parsing import parse"},{"note":"Use this for SQLServer-specific syntax, especially identifier handling with square brackets '[]'.","symbol":"parse_sqlserver","correct":"from mo_sql_parsing import parse_sqlserver"},{"note":"Use this for MySQL-specific syntax, such as handling double-quotes for literal strings.","symbol":"parse_mysql","correct":"from mo_sql_parsing import parse_mysql"}],"quickstart":{"code":"from mo_sql_parsing import parse\n\n# Basic SELECT query\nsql_query = \"select count(1) from jobs\"\nparsed_json = parse(sql_query)\nprint(f\"Parsed basic query: {parsed_json}\")\n# Expected: {'select': {'value': {'count': 1}}, 'from': 'jobs'}\n\n# SELECT with aliases\nsql_query_aliases = \"select a as hello, b as world from jobs\"\nparsed_json_aliases = parse(sql_query_aliases)\nprint(f\"Parsed query with aliases: {parsed_json_aliases}\")\n# Expected: {'select': [{'value': 'a', 'name': 'hello'}, {'value': 'b', 'name': 'world'}], 'from': 'jobs'}\n\n# Example with SQLServer-specific parsing\nfrom mo_sql_parsing import parse_sqlserver\nsqlserver_query = \"SELECT [Timestamp] FROM [table]\"\nparsed_sqlserver = parse_sqlserver(sqlserver_query)\nprint(f\"Parsed SQLServer query: {parsed_sqlserver}\")\n# Expected: {'select': 'Timestamp', 'from': 'table'}\n","lang":"python","description":"The `mo_sql_parsing` library's primary function is `parse`, which takes a SQL string and returns a JSON-like Python dictionary representing the parse tree. Specialized parsers like `parse_sqlserver` and `parse_mysql` are available for dialect-specific syntax."},"warnings":[{"fix":"Uninstall `moz-sql-parser` and install `mo-sql-parsing`. Update import statements: `from mo_sql_parsing import parse`.","message":"The `mo-sql-parsing` library is a fork of the now-archived `moz-sql-parser`. Users migrating from `moz-sql-parser` must update their package installations and import paths (`moz_sql_parser` to `mo_sql_parsing`).","severity":"breaking","affected_versions":"All versions (migration from `moz-sql-parser` to `mo-sql-parsing`)"},{"fix":"For SQLServer syntax, explicitly use `from mo_sql_parsing import parse_sqlserver as parse` to ensure correct parsing of identifiers.","message":"Square brackets `[]` have different meanings in SQL dialects (e.g., identifiers in SQLServer, array constructors in BigQuery). Using the default `parse` function for SQLServer queries might lead to incorrect interpretations.","severity":"gotcha","affected_versions":"All versions"},{"fix":"If Python's `None` is desired for `NULL` values, pass the `null=None` argument to the `parse` function: `parse(sql_query, null=None)`.","message":"By default, SQL `NULL` values are parsed into `{'null':{}}` in the output JSON tree, not Python's `None`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Consult the `mo-parsing` documentation for specific differences, especially regarding how parser actions modify grammar elements. For `add_parse_action()`, ensure the result is assigned back to the variable.","message":"This library relies on `mo-parsing` (a fork of `pyparsing`) internally. There are behavioral differences between `mo-parsing` and upstream `pyparsing`, notably how `add_parse_action()` creates new `ParserElement` objects that must be assigned, and `ParserElements` being static. Direct application of `pyparsing` idioms may not work.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Install `mo-sql-parsing` (`pip install mo-sql-parsing`) and update import statements to `from mo_sql_parsing import parse`.","cause":"The old `moz-sql-parser` library is not installed or the import path is incorrect. `mo-sql-parsing` is its successor.","error":"ModuleNotFoundError: No module named 'moz_sql_parser'"},{"fix":"To get Python's `None` for SQL `NULL`, call the `parse` function with the `null` argument: `result = parse(sql, null=None)`.","cause":"The default behavior of `mo-sql-parsing` is to represent SQL `NULL` as a dictionary `{'null':{}}` in the parse tree.","error":"Unexpected JSON output for SQL 'NULL' as {'null': {}} instead of None."},{"fix":"Import and use the `parse_sqlserver` function explicitly for SQLServer queries: `from mo_sql_parsing import parse_sqlserver as parse`.","cause":"The default parser often assumes a BigQuery-like dialect where `[]` denotes array constructors. This conflicts with SQLServer's use of `[]` for identifiers.","error":"SQL queries with square brackets like `SELECT [col] FROM [tbl]` are not parsed correctly; they might be interpreted as array literals."}]}