3

How to override a column of JSON type that has value = NULL to empty json, {}?

UPDATE `table`
SET `column_json` = JSON_SET(NULL, '{}', '{}')
WHERE
    `condition` = 1;

1 Answer 1

3

I think you can simply check for IS NULL in the WHERE clause, and then set the JSON column to string {}. MySQL should be able to implicitly typecast the string literal (which is also a valid JSON literal) to JSON:

UPDATE `table`
SET `column_json` = '{}'
WHERE
    `condition` = 1 
     AND `column_json` IS NULL 
Sign up to request clarification or add additional context in comments.

2 Comments

And this should work because a string literal should also be a valid JSON literal.
Hey Madhur... congratulations for earning the mysql gold badge!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.