Thomas Jalabert
Thomas Jalabert

Reputation: 1446

How to add a value to a JSON Array in a MySQL Table

I have a JSON typed column and I try to use the native JSON functions that MySQL allow me to use: https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html

I am especially looking for a way to update the JSON. Adding several values in the same JSON Array. The documentation is a bit rough on this point.

I know that it's not recommended to store JSON in a DB but I don't have the choice.

Upvotes: 1

Views: 3096

Answers (1)

Thomas Jalabert
Thomas Jalabert

Reputation: 1446

After a few minutes I managed to create my query:

UPDATE table 
SET field=JSON_ARRAY_INSERT(field, "$[0]", JSON_OBJECT('c', 0.5, 'd', 0.9)) 
WHERE id=12;

I'm sure I will forget this, so I created this page for my future self.

Good luck and Have fun!

Upvotes: 8

Related Questions