I have JSON string stored in my database column. I have to update that value in JSON string.
Here Is my table.
I want to update the state
value inside it.
Name1 has State value
KA
so I want to update it toGJ
.
UPDATE Customer
SET Detail = JSON_MODIFY(Detail , '$.Address.State', 'KA')
WHERE Name = 'name1';
Also Tried JSON_REPLACE
is also not working.
FUNCTION Customer.JSON_MODIFY does not exist
Note: I know one workaround to do this but I didn't Want to fetch that string and update it completely. I want to update the particular detail in string.
I have also created the SQL Fiddle.
I am doing this on localhost. Below are the localhost detail.
Database server
Server: localhost (localhost via TCP/IP)
Software: MySQL
MySQL Version :5.5.24
phpMyAdmin
Version information: 3.5.1, latest stable version: 4.7.3
Array value of a JSON object can be modified. It can be simply done by modifying the value present at a given index. Note: If value is modified at an index which is out of the array size, then the new modification will not replace anything in the original information but rather will be an add-on.
You can use the UPDATE statement to modify values of a JSON column in the SET clause. You can only update the JSON column, not individual portions of the JSON instance. Also, when referencing a JSON column in its entirety, the input format is the same as the INSERT statement. MERGE.
A JSON object contains zero, one, or more key-value pairs, also called properties. The object is surrounded by curly braces {} . Every key-value pair is separated by a comma. The order of the key-value pair is irrelevant. A key-value pair consists of a key and a value, separated by a colon ( : ).
12.16 JSON Functions
...
Unless otherwise indicated, the JSON functions were added in MySQL 5.7.8.
...
Try:
UPDATE `Customer`
SET `Detail` = JSON_REPLACE(`Detail`, '$.Address.State', 'GJ')
WHERE `Name` = 'name1';
See db-fiddle.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With