I am having trouble updating MySQL5.7.x JSON data.
I have the following JSON stored in the field activities. I want to search for activities where the outcome_id = 418
My table 2017_assessment_data has one record with student_id field (value of 3531) and the activities JSON field with the following data:
{
"course": "ENGLISH",
"level" : "2",
"activities": [
{
"comments": "test1",
"outcomes": [
{
"outcome_id": "423",
"course": "ENGLISH",
"course_level": "2",
"internal_outcome_id": "1"
}
],
"activity_name": "Quiz from chapters 1,2",
"date_completed": "20180201"
},
{
"comments": "test1 comments",
"outcomes": [
{
"outcome_id": "421",
"course": "ENGLISH",
"course_level": "2",
"internal_outcome_id": "4"
},
{
"outcome_id": "415",
"course": "ENGLISH",
"course_level": "2",
"internal_outcome_id": "5"
}
],
"activity_name": "Test chapter 4",
"date_completed": "20180201"
},
{
"comments": "test1",
"outcomes": [
{
"outcome_id": "426",
"course": "ENGLISH",
"course_level": "2",
"internal_outcome_id": "4"
},
{
"outcome_id": "418",
"course": "ENGLISH",
"course_level": "2",
"internal_outcome_id": "3"
}
],
"activity_name": "Activity",
"date_completed": "20180201"
},
{
"comments": "",
"outcomes": [],
"activity_name": "NEW",
"date_completed": ""
}
]
}
I've added an empty activity at the bottom of the JSON data, but I can't later edit the values.
Here is my PHP code:
$update = "
UPDATE 2017_assessment_data
SET
assessment_data = JSON_SET(assessment_data,'$.activities.activity_name','NEW ACTIVITY NAME')
WHERE
student_id = '3531'
AND
JSON_EXTRACT(assessment_data, '$.course') = 'ENGLISH'
AND
JSON_EXTRACT(assessment_data, '$.level') = '2'
AND
JSON_EXTRACT(assessment_data, '$.activities[*].activity_name') = 'NEW'
";
Any ideas??