Common SQL Errors

MySQL Error 3145 ER_INVALID_JSON_CHARSET_IN_FUNCTION: How to Fix and Prevent

Galaxy Team
August 8, 2025

The error appears when a JSON function receives character data that is not encoded in utf8 or utf8mb4.

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 3145 ER_INVALID_JSON_CHARSET_IN_FUNCTION?

MySQL error 3145 ER_INVALID_JSON_CHARSET_IN_FUNCTION occurs when a JSON function gets non-UTF8 text. Convert the input string or column to utf8mb4, or cast it with CAST(col AS CHAR CHARACTER SET utf8mb4), then rerun the query to resolve the issue.

Error Highlights

Typical Error Message

ER_INVALID_JSON_CHARSET_IN_FUNCTION

Error Type

Data Type Error

Language

MySQL

Symbol

%s'; utf8 is required. ER_INVALID_JSON_CHARSET_IN_FUNCTION was added in 5.7.8.

Error Code

3145

SQL State

22032

Explanation

Table of Contents

What is MySQL error 3145 ER_INVALID_JSON_CHARSET_IN_FUNCTION?

Error 3145 fires when any MySQL JSON function, such as JSON_OBJECT or JSON_EXTRACT, processes a string that is not encoded in utf8 or utf8mb4. MySQL enforces UTF8 family encodings for JSON data because the internal JSON binary format assumes Unicode.

The server halts the statement, returns SQL state 22032, and shows the message "Invalid JSON character data provided to function ...; utf8 is required." This validation first appeared in MySQL 5.7.8.

What Causes This Error?

The most common trigger is passing a column defined with latin1, cp1252, or another legacy charset into a JSON_ function without converting it. Even a single varchar literal in a different charset can cause the exception.

The error also appears when CONCAT, GROUP_CONCAT, or stored procedure variables assemble mixed-charset strings that ultimately feed a JSON function. The last evaluated character set governs the entire expression.

How to Fix ER_INVALID_JSON_CHARSET_IN_FUNCTION

Convert every argument to utf8mb4 before the JSON call. Use CAST( ... AS CHAR CHARACTER SET utf8mb4) or the CONVERT(expr USING utf8mb4) function. For columns, consider permanently altering them to utf8mb4 to avoid repeated conversions.

If the source data must remain in another charset, wrap only the JSON-bound expression in a conversion: JSON_OBJECT('name', CONVERT(name USING utf8mb4)).

Common Scenarios and Solutions

Legacy tables created in latin1 often break new JSON code added during migrations. Bulk ALTER TABLE ... CONVERT TO CHARACTER SET utf8mb4 fixes the root cause and simplifies future development.

APIs that accept client-side text sometimes insert values tagged as utf8 but actually encoded differently. Enforce connection collation utf8mb4 and enable strict SQL modes to reject bad input early.

Best Practices to Avoid This Error

Create all new schemas, tables, and connections with default_character_set=utf8mb4 and default_collation=utf8mb4_0900_ai_ci. This eliminates charset mismatches for JSON work.

Validate incoming text at the application layer and log any conversion warnings. Galaxy users can run automated lint rules that flag non-UTF8 columns referenced by JSON functions, catching issues before they reach production.

Related Errors and Solutions

Error 3144 ER_INVALID_JSON_TEXT reports malformed JSON rather than charset issues. Error 3146 ER_INVALID_JSON_PATH_CHARACTER_SET arises when paths, not values, use the wrong charset. The fixes are similar: ensure utf8mb4 encoding.

Common Causes

Passing latin1 columns to JSON functions

Varchar or text columns defined in latin1 feed JSON_OBJECT without conversion.

Mixing charsets in concatenated strings

CONCAT of utf8mb4 and latin1 expressions adopts the first argument charset, causing hidden issues.

Client connection using wrong collation

Applications connecting with character_set_client=latin1 insert data that later breaks JSON queries.

Related Errors

Error 3144 ER_INVALID_JSON_TEXT

Triggers on malformed JSON syntax rather than charset.

Error 3146 ER_INVALID_JSON_PATH_CHARACTER_SET

Raised when a JSON path string is not utf8.

Error 3147 ER_INVALID_JSON_PATH_WILDCARD

Indicates illegal wildcard characters in the JSON path expression.

FAQs

Can I disable the charset check?

No. MySQL hard-enforces UTF8 for JSON to guarantee correct binary storage. Workarounds require converting the data.

Is utf8 acceptable or must I use utf8mb4?

utf8mb4 is recommended because MySQL's utf8 encoding is limited to three-byte Unicode. Both pass the charset check, but utf8mb4 supports full Unicode.

Will casting hurt performance?

Inline CAST adds minimal overhead on small strings. Converting the column at rest avoids repeated per-row conversions and usually performs better overall.

How does Galaxy help?

Galaxy highlights charset mismatches in the editor and suggests ALTER statements or CAST wrappers via its AI copilot, preventing runtime errors.

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