1

We have Student table which contains JSON in 'jsonData' column(longtext). We need to change value of "isHandicapped" field in json.

Structure:

{
  "data": {

    "schoolData": {

      "studentListe": [
        {
          "student": {
            "studentId": 111749,
            "isHandicapped": false
            }
        }
      ],
    },
  }
}

Old Data: "isHandicapped": false

New Value: "isHandicapped": "NO"

This should be conditional update as for true we need to change value as

Old Data: "isHandicapped": true

New Value: "isHandicapped": "Ja"

Primary field of table is ID.

I got following SQL Query for same but unable to understand how to add conditional update that if value is true put Ja else NO:

update Student 
set data = JSON_SET(data, "'$."data"."schoolData"."studentListe"[*]."isHandicapped", "?") 
where id = 2;

2 Answers 2

2

Here's how you would extract that field:

select json_extract(data, '$.data.schoolData.studentListe[*].student.isHandicapped') from Student;
+-------------------------------------------------------------------------------+
| json_extract(data, '$.data.schoolData.studentListe[*].student.isHandicapped') |
+-------------------------------------------------------------------------------+
| [false]                                                                       |
+-------------------------------------------------------------------------------+

You can use JSON_SET() to set a value for a specific array member 0:

update student set data = json_set(data, '$.data.schoolData.studentListe[0].student.isHandicapped', 'NO');
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

select json_extract(data, '$.data.schoolData.studentListe[*].student.isHandicapped') from Student;
+-------------------------------------------------------------------------------+
| json_extract(data, '$.data.schoolData.studentListe[*].student.isHandicapped') |
+-------------------------------------------------------------------------------+
| ["NO"]                                                                        |
+-------------------------------------------------------------------------------+

But you can't use the * wildcard to update all array members:

update student set data = json_set(data, '$.data.schoolData.studentListe[*].student.isHandicapped', '"NO"');
ERROR 3149 (42000): In this situation, path expressions may not contain the * and ** tokens or an array range.

I see a lot of questions on Stack Overflow these days about selecting or modifying JSON data in MySQL. Usually the answer is:

You shouldn't store data in JSON format if you need to select or update fields within the JSON document.

This type of task is easy to solve if you stored your data in normal tables and columns:

UPDATE StudentListe
SET isHandicapped = 'NO'
WHERE studentId = 2;
2
  • You shouldn't store data in JSON format if you need to select or update fields within the JSON document. So in what cases it can be helpful?
    – James
    Dec 15, 2019 at 11:07
  • @James If a JSON document comprises a whole, single object that you always insert, update, or fetch in its entirety. Don't use SQL expressions to try to access sub-fields within the JSON. Use application code to serialize/deserialize the JSON into structs in your app, but your SQL code should treat it as a black box. Dec 15, 2019 at 17:56
1

The following update statement will do the trick:

UPDATE Student 
SET $.isHandicapped= CASE          
                     WHEN JSON_EXTRACT(data, "'$."data"."schoolData"."studentListe"[*]."isHandicapped")='false' 
                     THEN 'NO' 
                     ELSE 'Ja'  end
WHERE id=2;
1
  • 1
    UPDATE Student SET $.isHandicapped= 'NO' WHERE JSON_SEARCH(data, "'$."data"."schoolData"."studentListe"[]."isHandicapped"='false'; or where JSON_EXTRACT(data, "'$."data"."schoolData"."studentListe"[]."studentId")=111749 Feb 16, 2017 at 6:40

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.