Error
Error Code:
3147
MySQL Error 3147: Invalid Value for JSON Cast
Description
MySQL Error 3147 occurs when the database attempts to convert a value into the JSON data type but encounters an input that is not a valid JSON string or cannot be implicitly cast. This typically happens during `CAST(... AS JSON)` operations when the source data's format, structure, or content is incompatible with JSON specifications.
Error Message
Cannot CAST value to JSON.
Known Causes
3 known causesMalformed JSON String Input
The value being cast to JSON is syntactically incorrect, missing essential JSON elements like quotes, brackets, or commas.
Incompatible Data Type
The source value is of a data type (e.g., a binary string, an unsupported object type) that MySQL cannot convert into a valid JSON representation.
Invalid Character Encoding
The input string contains characters that are not valid UTF-8, which is a requirement for JSON data in MySQL.
Solutions
3 solutions available1. Ensure Valid JSON Structure for Casting easy
Correct the input string to conform to valid JSON syntax before casting.
1
Review the string you are attempting to cast to JSON. Ensure it follows the JSON specification (e.g., keys and string values are enclosed in double quotes, proper use of colons and commas, correct nesting of objects and arrays).
2
If the string is not valid JSON, manually correct it. For example, if you have single quotes for keys or values, replace them with double quotes. Remove trailing commas.
SELECT CAST('{"name": "John Doe", "age": 30}' AS JSON); -- Valid JSON
-- SELECT CAST('{name: "John Doe", age: 30}' AS JSON); -- Invalid JSON (keys not quoted)
-- SELECT CAST('{"name": "John Doe", "age": 30,}' AS JSON); -- Invalid JSON (trailing comma)
3
If the data originates from a variable or a column, ensure the data within that source is already properly formatted as a JSON string. If not, you might need to pre-process it.
UPDATE your_table SET json_column = CAST(REPLACE(REPLACE(your_string_column, '''', '"'), ',', '') AS JSON) WHERE ...; -- Example for simple string correction, use with caution!
2. Utilize JSON_VALID() for Pre-validation easy
Check if a string is valid JSON before attempting a cast to prevent the error.
1
Use the `JSON_VALID()` function to check if a string is a valid JSON document. This function returns 1 if the string is valid JSON, and 0 otherwise.
SELECT JSON_VALID('{"name": "John Doe", "age": 30}'); -- Returns 1
SELECT JSON_VALID('{name: "John Doe", age: 30}'); -- Returns 0
2
In your application logic or SQL queries, conditionally cast only if `JSON_VALID()` returns true. This prevents the error from occurring by avoiding invalid casts.
SELECT
CASE
WHEN JSON_VALID(your_string_column) THEN CAST(your_string_column AS JSON)
ELSE NULL -- Or some other default value
END AS json_output
FROM your_table;
3. Handle Non-JSON Data Gracefully medium
Implement logic to handle cases where the data is not JSON, rather than forcing a cast.
1
Identify the source of the data that is causing the casting error. This could be a column in a table, a variable, or external input.
2
If the data is expected to be JSON but might sometimes be malformed or entirely non-JSON, add error handling to your application code or use `CASE` statements in SQL to check for validity before casting.
SELECT
CASE
WHEN your_string_column IS NULL THEN NULL
WHEN JSON_VALID(your_string_column) THEN CAST(your_string_column AS JSON)
ELSE 'Invalid JSON Data' -- Or log an error, or return a specific marker
END AS processed_json
FROM your_table;
3
Consider if the non-JSON data should be transformed into JSON. If so, implement the necessary transformation logic. If it's truly non-JSON and should be treated as such, assign it a default value or handle it as an error.