I'm trying to change one of the array-valued keys (called religions) of a JSON field named preferences in a MySQL table.
I'm not using JSON_ARRAY_APPEND because the changes in the field can be arbitrary and can involve both removals and additions.
Right now I'm trying to change it by using the following query.
const religionPreferences = ["Buddhism","Christianity","Non religious"]
const sql = `UPDATE users SET preferences = JSON_REPLACE(
preferences,
'$.${key}',
JSON_ARRAY(religionPreferences[0], religionPreferences[1], religionPreferences[2])
)
WHERE id = "${req.params.id}" LIMIT 1`
The problem is that the religionPreferences array can have anywhere from 0-14 elements and I wasn't sure how to proceed with variable arguments having to be passed into the JSON_ARRAY function.
const religionPreferences = ["Buddhism","Christianity","Non religious"]
const sql = `UPDATE users SET preferences = JSON_REPLACE(
preferences,
'$.religions',
JSON_ARRAY(religionPreferences[0], religionPreferences[1], religionPreferences[2])
)
WHERE id = "${req.params.id}" LIMIT 1`
Another approach I tried was just directly setting the argument for JSON_REPLACE as an array literal instead of passing in JSON_ARRAY as below.
const religionPreferences = ["Buddhism","Christianity","Non religious"]
const sql = `UPDATE users SET preferences = JSON_REPLACE(
preferences,
'$.religions',
${religionPreferences}
)
WHERE id = "${req.params.id}" LIMIT 1`
The problem is that I want it to be saved as
religions: ["Christian","Buddhist","Other"]
instead of
religions: '["Christian","Buddhist","Other"]'
But when I tried to save it as an array directly without quotes around it, it said the SQL had an error..
Thanks so much. -Jay