I have the following data in a meta json column and need to rename the Visual Arts interests.
{
"industries": [
{
"name": "Science",
"id": 1,
"interests": [
{
"id": 1,
"name": "Visual Arts"
},
{
"id": 2,
"name": "Data Science"
}
]
},
{
"name": "Arts",
"id": 2,
"interests": [
{
"id": 3,
"name": "Music"
},
{
"id": 1,
"name": "Visual Arts"
}
]
}
]
}
I have managed to rename the industry name with this statement:
UPDATE table
SET meta = JSON_REPLACE(meta, JSON_UNQUOTE(JSON_SEARCH(meta, 'all', 'old_name')), 'new_name')
This works because the JSON_SEARCH returns one path: $.industries[0].name
How can I update the interests if there are multiple values to be updated?
Running the above update statement to update the Visual Arts interest results in an error:
Query 1 ERROR: Invalid JSON path expression. The error is around character position 1.
I assume it's because there are two paths returned from the statement:
SELECT JSON_SEARCH(meta, 'all', 'Visual Arts')
FROM table
Results in:
["$.industries[0].interests[0].name", "$.industries[1].interests[1].name"]
Any help would be appreciated.
Thanks in advance.
Edit This query works:
SELECT JSON_REPLACE(meta, '$.industries[0].interests[0].name', 'new_name', '$.industries[1].interests[1].name', 'new_name')
FROM job_requirements
Is there a way to convert
["$.industries[0].interests[0].name", "$.industries[1].interests[1].name"]
into
'$.industries[0].interests[0].name', 'new_name', '$.industries[1].interests[1].name', 'new_name'
SELECT VERSION();return? I'd like to know if you can useJSON_TABLE(), which requires MySQL 8.0.