MySQL raises ER_JSON_DOCUMENT_TOO_DEEP (SQLSTATE 22032) when a JSON document contains more than 100 nested levels.
ER_JSON_DOCUMENT_TOO_DEEP means your JSON has more than 100 nested levels. Flatten or split the document, or on MySQL 8.0.17+ increase json_depth_limit to resolve the error.
ER_JSON_DOCUMENT_TOO_DEEP
MySQL raises error code 3157 with SQLSTATE 22032 when it encounters a JSON value nested deeper than the allowed limit (100 by default). The server stops the operation to avoid stack overflow and performance degradation.
The error can appear in SELECT, INSERT, UPDATE, or function calls such as JSON_EXTRACT, JSON_SET, and JSON_ARRAYAGG whenever the evaluated document exceeds the depth limit.
Applications that serialise complex objects without depth checks often create deeply nested arrays and objects that breach the limit.
Recursive CTEs or stored procedures that build hierarchical JSON incrementally can push depth beyond 100 levels.
Flatten or split the JSON structure so no branch is deeper than the configured json_depth_limit. Store child objects in separate rows and link them using foreign keys.
In MySQL 8.0.17+ change the limit for the session or globally when high depth is genuinely required.
-- Allow deeper JSON for this session
SET SESSION json_depth_limit = 200;
-- Persistent change (requires SUPER privilege)
SET GLOBAL json_depth_limit = 200;
-- Confirm the setting
SELECT @@json_depth_limit;
If you run 5.7.8 - 8.0.16, where the variable is unavailable, restructure the data instead of altering server configuration.
Bulk imports: validate each document with JSON_DEPTH() in a staging table and reject those exceeding the threshold.
API ingestion: add middleware to check nesting levels before writing to MySQL.
Design schemas that keep hierarchical data relational; use adjacency lists for trees rather than deeply nested JSON.
Enforce depth limits in the application layer and add unit tests that load representative payloads.
Monitor error logs and metrics for spikes in ER_JSON_DOCUMENT_TOO_DEEP to catch regressions early.
ER_INVALID_JSON_TEXT appears when the JSON syntax is malformed; run JSON_VALID() to locate issues.
ER_INVALID_JSON_PATH_KEY occurs when a supplied JSON path is invalid; correct the path expression.
ER_DUP_ENTRY occurs when unique keys clash during restructuring; ensure primary keys cover new rows after splitting JSON.
Applications serialising parent child objects without a depth guard can quickly exceed 100 levels.
Building arrays of arrays in reporting queries leads to deep structures when embedded inside larger documents.
Combining multiple external JSON fragments may accidentally produce extreme nesting even if each source file is valid.
Servers prior to 8.0.17 cannot raise the depth limit, so any payload deeper than 100 levels triggers the error.
Raised when the JSON is syntactically invalid - fix malformed characters or missing commas.
Occurs when a JSON path expression is invalid - modify the path string to a supported syntax.
Triggered when JSON_SET or JSON_REPLACE cannot locate the specified path - verify that the key or index exists before calling the function.
Versions 5.7.8 and later enforce a default maximum depth of 100 nested levels.
No. The json_depth_limit variable is only available in MySQL 8.0.17 and newer; earlier releases require restructuring the JSON.
A higher limit increases memory consumption for deep documents; monitor resource usage and test before deploying to production.
Galaxy's AI copilot validates JSON depth while autogenerating INSERT or UPDATE statements and warns users when payloads may exceed the server limit.