Given the following JSON stored inside a MySQL json data type:
"users": [
{
"group": "manager",
"userID": "a123"
},
{
"group": "employee",
"userID": "a456"
}
]
How can I remove user object with "userID": "a456" without knowing its position in the array?
If we know the position then the following works:
$query = 'UPDATE jsontable SET
jsondata = JSON_REMOVE
(
jsondata,
"$.users[1]"
);';
However, what I'm looking for is something like this:
$query = 'UPDATE jsontable SET
jsondata = JSON_REMOVE
(
jsondata,
(
JSON_SEARCH
(
jsondata,
"all",
JSON_OBJECT("userID", "a456")
)
)
);';
The above produces a data truncated error for the jsondata column. Other variations such as putting the userID directly instead of JSON_OBJECT also produce errors.
What do I need to fix in the query to make JSON_SEARCH return the path to the specific object that I want to remove from the array?
Bearing in mind that it should only search inside of users, as there might be other properties on the main JSON object using those IDs.