Common SQL Errors

MySQL Error 3157 ER_JSON_DOCUMENT_TOO_DEEP: How to Fix and Prevent

Galaxy Team
August 8, 2025

MySQL raises ER_JSON_DOCUMENT_TOO_DEEP (SQLSTATE 22032) when a JSON document contains more than 100 nested levels.

Sign up for the latest in common SQL errors from the Galaxy Team!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

What is MySQL error ER_JSON_DOCUMENT_TOO_DEEP?

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.

Error Highlights

Typical Error Message

ER_JSON_DOCUMENT_TOO_DEEP

Error Type

Data Validation Error

Language

MySQL

Symbol

ER_JSON_DOCUMENT_TOO_DEEP was added in 5.7.8.

Error Code

3157

SQL State

22032

Explanation

Table of Contents

Why am I seeing 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.

What Causes This Error?

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.

How to Fix ER_JSON_DOCUMENT_TOO_DEEP

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.

Common Scenarios and Solutions

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.

Best Practices to Avoid This Error

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.

Related Errors and Solutions

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.

Common Causes

Uncontrolled recursive data structures

Applications serialising parent child objects without a depth guard can quickly exceed 100 levels.

Excessive JSON_ARRAYAGG nesting

Building arrays of arrays in reporting queries leads to deep structures when embedded inside larger documents.

Merging third party payloads

Combining multiple external JSON fragments may accidentally produce extreme nesting even if each source file is valid.

Old MySQL versions

Servers prior to 8.0.17 cannot raise the depth limit, so any payload deeper than 100 levels triggers the error.

Related Errors

ER_INVALID_JSON_TEXT (1153)

Raised when the JSON is syntactically invalid - fix malformed characters or missing commas.

ER_INVALID_JSON_PATH (3143)

Occurs when a JSON path expression is invalid - modify the path string to a supported syntax.

ER_DOCUMENT_NOT_FOUND (3162)

Triggered when JSON_SET or JSON_REPLACE cannot locate the specified path - verify that the key or index exists before calling the function.

FAQs

How many levels does MySQL allow by default?

Versions 5.7.8 and later enforce a default maximum depth of 100 nested levels.

Can I change the limit in MySQL 5.7?

No. The json_depth_limit variable is only available in MySQL 8.0.17 and newer; earlier releases require restructuring the JSON.

Does raising json_depth_limit harm performance?

A higher limit increases memory consumption for deep documents; monitor resource usage and test before deploying to production.

How does Galaxy prevent this error?

Galaxy's AI copilot validates JSON depth while autogenerating INSERT or UPDATE statements and warns users when payloads may exceed the server limit.

Start Querying with the Modern SQL Editor Today!
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Check out some other errors

Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo