How to Fix MySQL Error 3157 (ER_JSON_DOCUMENT_TOO_DEEP)
You get mysql error 3157 when a JSON object or array nests too deep. You fix it by making your JSON data flatter before you save it. The database engine uses the symbol ER_JSON_DOCUMENT_TOO_DEEP and SQLSTATE 22032 for this failure. Its message states that the JSON document exceeds the maximum depth. When the parser hits this hard limit, it stops the query right away. This rule keeps the server from running out of stack memory during parsing. What causes mysql error 3157 in the database engine
Your coding agents run with your access.
Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.
You get mysql error 3157 when a JSON object or array nests too deep. You fix it by making your JSON data flatter before you save it.
The database engine uses the symbol ER_JSON_DOCUMENT_TOO_DEEP and SQLSTATE 22032 for this failure. Its message states that the JSON document exceeds the maximum depth. When the parser hits this hard limit, it stops the query right away. This rule keeps the server from running out of stack memory during parsing.
What causes mysql error 3157 in the database engine
Every open brace or bracket in a JSON string adds one level of depth. When an app nests objects inside objects too many times, the engine stops. This often happens when code has a broken loop that wraps data again and again. It also happens when an app saves big tree structures in one JSON column.
Sometimes a message queue or webhook sends a bad payload from an outside service. If your app passes that raw string straight to the database, the write fails. You can also trigger this error inside the database by merging two deep JSON columns. If the combined result passes the depth limit, the update query fails and rolls back.
The engine checks depth on both reads and writes of JSON values. Even a select query that builds a deep JSON object on the fly will fail. Because this limit is built into the engine, you cannot raise it with a setting. You must change the shape of the data or how your app stores it.
How to find and fix deep JSON safely in production
Start by checking your app error logs to find the exact query that failed. Look at the code path that builds the JSON payload for that query. Do not grant broad database roles to devs just to inspect live data. Instead, let them check the payload shape in a test setup or staging log.
If you must inspect live rows to see existing depth, use a safe read role. Make sure data masking rewrites matched fields like emails, phone numbers, or card numbers in results. This lets the team check JSON structure and depth without exposing sensitive user details.
JSON Web Tokens (JWT) + MySQL Access Governance: architecture patterns and best practices
Continue reading? Get the full guide.
Free. No spam. Unsubscribe anytime.
Once you find the code that builds the deep object, flatten the structure. Turn deep trees into a flat list of items where each item holds a parent key. Better yet, move core fields out of the JSON column into standard relational tables. Relational rows and foreign keys handle deep trees much better than a single JSON blob.
How to stop deep JSON errors before they hit the database
Your app should check the depth of every JSON input before running a query. Add a simple check in your API layer that counts nesting levels on incoming payloads. If a user or webhook sends an object that nests too deep, reject the request early. Returning a clear client error at the API edge keeps bad data out of your database.
You should also set up unit tests that check your JSON serializers. Make sure object mappers do not follow circular links between parent and child records. Circular links are a top cause of runaway nesting in backend apps. Fixing the serializer stops mysql error 3157 for good and keeps queries fast.
Check MySQL queries and results on the wire
MySQL grants control who logs in. They do not check single queries or mask sensitive columns once a session starts. With hoop.dev, you run an open-source proxy, a sidecar for your MySQL, inline in the MySQL wire protocol. On every query an AI agent or human runs against production MySQL, RDS, or Aurora, hoop.dev decides to pass, mask, block, or hold it for review.
Multi-statement batches and prepared queries are split and checked one by one by hoop.dev guardrails, which refuse banned queries with a native MySQL error or hold risky ones for approval. On the response path, hoop.dev data masking rewrites string values in returned rows in memory, and every decision is logged. See the MIT-licensed project on GitHub.
Frequently asked questions
Can I change a server setting to allow deeper JSON in MySQL?
No, the maximum JSON depth is a hard limit built into the database engine. You must flatten your JSON data or store the items in normal tables instead.
Does mysql error 3157 happen during select queries too?
Yes, it can happen if a select query uses functions that build or merge JSON values. If the new JSON value passes the depth limit, the read query fails right away.
How do I find which part of my JSON payload is too deep?
Run a small script in your app code to count nesting levels for each key. Log the path with the highest count so you know which part to flatten.
Related guides
In this post