Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

modify the property value of JSON string stored in the Table Column

I have JSON string stored in my database column. I have to update that value in JSON string.

Here Is my table. my-table-image

I want to update the state value inside it.

Example:

Name1 has State value KA so I want to update it to GJ.

What I have Tried So far?

UPDATE Customer
SET Detail = JSON_MODIFY(Detail , '$.Address.State', 'KA')
WHERE Name = 'name1';

Also Tried JSON_REPLACE is also not working.

But it shows the error:

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
like image 373
always-a-learner Avatar asked Aug 06 '17 13:08

always-a-learner


People also ask

How do I change the value of a JSON object?

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.

How do I update a JSON column in SQL?

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.

What is property value in JSON?

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 ( : ).


1 Answers

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.

like image 178
wchiquito Avatar answered Sep 28 '22 04:09

wchiquito