Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

MySQL, json, JSON_ARRAY_APPEND - why will append not work on 5.7.13?

Tags:

json

mysql

How on earth do I insert data into a json mysql field.

I can create a record

INSERT INTO git_object (user_id,git_meta,last_update) VALUES ('11111','[{"host": "a", "id": "1"}]',(select now()));

How do I append to the array.

SELECT JSON_ARRAY_APPEND(git_meta, '$', '{"host": "b"}') FROM git_object where user_id='11111'

I tried the above. What do I get with the below?

SELECT * FROM git_object;

'[{"id": "1", "host": "a"}]'

What am I doing wrong

MySQL Version '5.7.13'

like image 855
Tampa Avatar asked Nov 28 '22 06:11

Tampa


2 Answers

To modify data stored in any table you need to use the update sql command, not select.

Update git_object
set  git_meta=JSON_ARRAY_APPEND(git_meta, '$', '{"host": "b"}')
where user_id='11111'
like image 73
Shadow Avatar answered Dec 23 '22 09:12

Shadow


Use JSON_ARRAY_APPEND(git_meta, '$', JSON_OBJECT('host','b'))

instead of JSON_ARRAY_APPEND(git_meta, '$', '{"host": "b"}')

like image 21
Christian Soutou Avatar answered Dec 23 '22 10:12

Christian Soutou