0

I'd like to insert this JSON value ["Chien", "Chat"] in a JSON, with MySQL8 built-in JSON functions. So I tried with this query but I didn't get the expected output:

UPDATE tasks SET outputFields = JSON_SET(outputFields, '$.checkbox', '["Chien","Chat"]') WHERE id = 6832

This query made this valid JSON:

{"key1": "value1", "key2": "value2", "key3": "value3", "checkbox": "[\"Chien\",\"Chat\"]"}

The expected value is:

{"key1": "value1", "key2": "value2", "key3": "value3", "checkbox": ["Chien","Chat"]}

Is there a function or a work arround for it?

Bill Karwin
  • 538,548
  • 86
  • 673
  • 828
Julien
  • 99
  • 1
  • 13
  • 1
    This is almost the same question as this one which I answered: https://stackoverflow.com/questions/69622166/how-do-i-change-an-array-valued-key-of-a-json-field-in-a-mysql-database-using-js/69622594#69622594 – Bill Karwin Sep 30 '22 at 14:34
  • It works with Cast function. Thank you Bill – Julien Sep 30 '22 at 15:30

1 Answers1

0

Just use CAST function like this:

UPDATE tasks SET outputFields = JSON_SET(outputFields, '$.checkbox', cast('["Chien","Chat"]' as json)) WHERE id = 6832
Julien
  • 99
  • 1
  • 13