search

Home  >  Q&A  >  body text

Correct syntax to query JSON_SET in MYSQL in Node

<p>I need to set/update a JSON array in a MYSQL table from Node and I have this query which throws an <code>Invalid JSON path expression</code> error. For example, I want to find the object with key <code>2022-01-03</code> and if it exists, update its value to <code>O 08:00</code></p> <pre class="brush:php;toolbar:false;">UPDATE allemployees SET schedule = JSON_SET(schedule, '$.2022-01-03', 'O 08:00') WHERE name_cyr = 'John Doe' </pre> <p>The JSON in my table is as follows:</p> <pre class="brush:php;toolbar:false;">[{"2022-01-03": "H 08:00"}, [{"2022-01-04": " H 08:00"}] ]</pre></p>
P粉903052556P粉903052556470 days ago495

reply all(1)I'll reply

  • P粉851401475

    P粉8514014752023-09-01 14:07:18

    UPDATE allemployees
    SET schedule = JSON_SET(schedule, '$[0]."2022-01-03"', 'O 08:00') 
    WHERE name_cyr = 'John Doe';
    

    https://dbfiddle.uk/?rdbms=mysql_8.0&fiddle=2c59600049b4dfc1675c444a6da578bb

    1. Path"2022-01-03" contains dashes and must be enclosed in double quotes.

    2. The value to be set is not the superior value, but the component of the clear array element.

    reply
    0
  • Cancelreply