Common SQL Errors

MySQL Error 3158: ER_JSON_DOCUMENT_NULL_KEY - How to Fix "JSON documents may not contain NULL member names"

Galaxy Team
August 8, 2025

MySQL raises ER_JSON_DOCUMENT_NULL_KEY (error 3158, SQLSTATE 22032) when a JSON document contains an object key that is NULL or an empty string.

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 3158 (ER_JSON_DOCUMENT_NULL_KEY)?

ER_JSON_DOCUMENT_NULL_KEY occurs when MySQL parses a JSON object whose member name is NULL or empty. Replace NULL keys or convert the data before insertion to resolve the issue. The primary fix is to ensure every JSON object key is a valid, non-NULL string.

Error Highlights

Typical Error Message

ER_JSON_DOCUMENT_NULL_KEY

Error Type

JSON Validation Error

Language

MySQL

Symbol

ER_JSON_DOCUMENT_NULL_KEY was added in 5.7.8.

Error Code

3158

SQL State

22032

Explanation

Table of Contents

What is MySQL Error 3158 (ER_JSON_DOCUMENT_NULL_KEY)?

MySQL error 3158, condition ER_JSON_DOCUMENT_NULL_KEY, is raised when the server validates or manipulates a JSON document that contains an object member whose key is NULL or an empty string.

The error is accompanied by SQLSTATE 22032 and the text "JSON documents may not contain NULL member names." It was introduced in MySQL 5.7.8 and applies to every subsequent version, including 8.x.

When Does This Error Appear?

The error commonly appears during INSERT, UPDATE, or JSON function calls (such as JSON_SET or JSON_MERGE) when the supplied JSON string has an invalid object key.

It can also surface while loading data via LOAD DATA INFILE, importing dumps, or calling stored procedures that build JSON dynamically.

Why Is It Important to Fix Quickly?

Blocking writes due to malformed JSON halts ETL pipelines and application requests, leading to data loss or downtime.

Because the error indicates structurally invalid data, ignoring it risks corrupting business logic and downstream analytics that rely on JSON consistency.

Common Causes

NULL Keys in Source Data

Upstream applications sometimes serialize objects that include null or undefined property names, producing JSON like {"":123} or {null:123}.

Dynamic SQL Concatenation Bugs

Building JSON strings via CONCAT or string interpolation can accidentally leave a key blank when a variable is NULL.

Incorrect CAST From Other Formats

Converting XML, CSV, or relational rows into JSON with GROUP_CONCAT may yield empty keys when source column names are missing.

Legacy Data Imports

Older dumps from MySQL versions before strict JSON validation might already contain invalid keys that fail on reload in 5.7.8+.

Related Errors

Error 3140 (ER_INVALID_JSON_TEXT)

Raised when a JSON string is syntactically invalid, such as missing braces or quotes.

Error 3146 (ER_JSON_DOCUMENT_TOO_DEEP)

Triggered when a JSON document exceeds the maximum nesting depth of 100 levels.

Error 3065 (ER_INVALID_JSON_PATH_CHARSET)

Occurs when a JSON path expression contains characters that do not match the column's character set.

Error 3134 (ER_INVALID_JSON_PATH_ARRAY_CELL)

Raised when an array index in a JSON path is out of range or malformed.

FAQs

Can I ignore ER_JSON_DOCUMENT_NULL_KEY and store the data as text?

You can switch the column type to LONGTEXT, but you lose JSON functions, indexing, and validation. Fixing the data is strongly recommended.

Does MySQL automatically fix NULL keys?

No. MySQL rejects the statement entirely. You must supply valid JSON before the engine accepts it.

Which MySQL versions enforce this rule?

All versions from 5.7.8 onward enforce it, including the entire 8.x series.

How do I catch this error early in CI/CD?

Add JSON_VALID checks or unit tests in your CI pipeline, and use Galaxy's AI copilot to review migration scripts for JSON issues.

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