How do I edit a JSON within my database?

Asked

Viewed 260 times

4

I have a JSON saved in my database. I would like to know how to edit this json using PHP and saving it again in the database.

The json that would like to change the value is this

{"3":{"tipo":"Premium","valor":"100","quantidade":"200","vendas":"0","status":"1"},"1":{"tipo":"Arena","valor":"50","quantidade":"200","vendas":"0","status":"1"}}

I’d like to change the values Sales for 10 and Status for

2 answers

3

You can do direct by SQL through the functions for the type JSON:

UPDATE
    myTable
SET
    myColumn = JSON_SET(
        myColumn,
        '$.3.vendas', 10,
        '$.1.vendas', 10,
        '$.3.status', 0,
        '$.1.status', 0
    );

Remember that the JSON type and its functions are only available from version 5.7.8.

  • When you say "guy" means we can create a column like json?!

  • 2

    "type" is the die type of the column, instead of using VARCHAR, use JSON

  • @Guilhermecostamilam is true! I just had a test at the bank! I didn’t know that! I thought only in the new version on Nosql that this existed!

  • @Lipespry the type is JSON! =)

  • I’m gonna start using that brace! Thanks bro!

  • 1

    @Andreicoelho suspected from the beginning! (Our stupidity! You have the link’s Disgrama in the answer)

Show 1 more comment

1


You can use the array_map to browse the array created with json_decode and apply the change wherever you want:

    $string = '{"3":{"tipo":"Premium","valor":"100","quantidade":"200","vendas":"0","status":"1"},"1":{"tipo":"Arena","valor":"50","quantidade":"200","vendas":"0","status":"1"}}';

    // transforma em array
    $jsonArray = json_decode($string, true);

    $jsonArray = array_map(function($e){
         $e['vendas'] = 10; // altera a venda para 10
         $e['status'] = 2; // altera o status para 2
         return $e;
    }, $jsonArray);

    // transforma em json novamente
    echo json_encode($jsonArray);

Check it out at Ideone

Another option is to use the array_walk:

    array_walk($jsonArray, function(&$e, $k){
         $e['vendas'] = 10; // altera a venda para 10
         $e['status'] = 2; // altera o status para 2
    });

If you are going to change the values manually:

    $jsonArray = json_decode($string, true); // transforma em array
    $jsonArray[3]['vendas'] = 20; // altera determinado valor
    echo json_encode($jsonArray); // transforma em json
  • and if I want to edit only one of the parts? for example only the Premium type.

  • @Joãopedro did the editing

  • Another question, how do I get that value "3" from json??

  • @John Peter as well?

  • I made an edit to help.

  • @Joãopedro put the code at the end of the answer. But friend... When so, create a new question. = ) For soon, soon, you will mischaracterize it.

  • I wanted to take the [PREMIUM] array values (value, quantity...) and replace within the json type:"Premium" the values (value, quantity...)

  • @Joãopedro Now that I understand more or less what you want ...you better create a new question and detail there...

  • 1

    Okay, I created a new question to make things more organized.

  • @Joãopedro if you can, then ask the previous question... It can help other users.

Show 6 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.