I am trying to update a column in MySQL, where it contains a json encoded value with multiple key-value pairs, but cannot write the exact query. Can anyone help?
For example:
tableA:
id | response |
---|---|
1 | {"event_id":"11111","contact":{"contact_id":"as2344","answers": [{"answer_id":"2223e"}],"tags":"cadd76","question_id":"67888"} |
From the above row, I am trying to remove "answers" and "tags" from the key "contact", so that the result after the update query will be like below:
{"event_id":"11111","contact":{"contact_id":"as2344","question_id":"67888"}
I cannot find any method as to how to update that column. So please anyone can help?
2
Answers
You will have to do the plain string Manipulation here. Please use below reference to find out functions you need here.
https://dev.mysql.com/doc/refman/8.0/en/string-functions.html
Hope this helps you.
In MySQL, you can use
JSON_REMOVE()
for this since version 5.7:Or if you wanted an
update
statement:Demo
Note that modifying stored JSON in somehow a red-flag that indicate a data model problem; in many cases, it is much easier to properly model your relationships into database tables, and then use standard DML capabilities.