Question

how do i add the new attribute to a json object i have stored in a mysql table column? now i have json_encode data stored like:

"1":{"lastname":"blah","firstname":"R.A.","function":"Manager","email":"test@hotmail.com","barcode":"33432799181"}  

and i like to add a new data pair, visted:0 which i want to update as soon as a person has been visited.

"1":{"lastname":"blah","firstname":"R.A.","function":"Manager","email":"test@hotmail.com","barcode":"33432799181", "visited":"0"}

How do i push an element to the existing json data?

$jsondataTmp = json_decode($core,true);
$custom = array('visited'=>'0');
$jsondataTmp[] = $custom;

but it adds an array and not within each

  [1] => Array
        (
            [lastname] => blah
            [firstname] => R.A.
            [function] => Manager
            [email] => test@hotmail.com
            [barcode] => 33432799181
        )

    [2] => Array
        (
           [lastname] => blah
            [firstname] => R.A.
            [function] => Manager
            [email] => test@hotmail.com
            [barcode] => 33432799181
        )

    [3] => Array
        (
            [visited] => 0
        )

and not

[2] => Array
            (
               [lastname] => blah
                [firstname] => R.A.
                [function] => Manager
                [email] => test@hotmail.com
                [barcode] => 33432799181
[visited] => 0
            )
Was it helpful?

Solution

MySQL doesnot have native support for JSON - MySQL still is pure RDBMS system.

One easy hack for the time being can be using a REPLACE function.

UPDATE table_name
SET column_name = REPLACE ( column_name, "}", ',"visited":"0"}';
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top