php - UPDATE parts of JSON doc with JSON_SET() instead of setting whole doc new -


i want update parts of json doc inside mysql database table.

the field name in database: jdata

the problem

i do not want set jdata = '{_whatever_}', because loose keys.

i need preserve existing keys (e.g. key1, key3) , set key2 new designated value 999.

the json doc

{   "calculation":{     "key1": 100,     "key2": 200,     "key3": 300   } } 

the php/mysql

$a = json_encode(['key2' => 999], json_unescaped_slashes); $sql = "   update  svtr.auftrag     set     jdata = json_set(jdata, '$.calculation', '{$a}')     where[...] "; 

using full path so: json_set(jdata, '$.calculation.key2', 999) no option here, since original json doc more complex , have iterate many keys. think of having 30 key/value pairs , needing update 29 of them.

hope can make sense of ramblings ... in advance!

seems getting jdata fine, somehow like

$jdata = 'select jdata yourtable condition';  //let's assume got following data $jdata = '{     "calculation":{     "key1": 100,     "key2": 200,     "key3": 300   } }'; var_dump($jdata);  //now change specifi value //encode php object  $jdata = json_decode($jdata); //change specific value following $jdata->calculation->key2 = 555; //decode $changed_jdata = json_encode($jdata); var_dump($jdata);  $updatesql = 'update jdata = '.$changed_jdata; 

it still not solved problem. m here make that


Comments

Popular posts from this blog

Spring Boot + JPA + Hibernate: Unable to locate persister -

go - Golang: panic: runtime error: invalid memory address or nil pointer dereference using bufio.Scanner -

c - double free or corruption (fasttop) -