futurefuture's avatar

Stupid Question - JSON_ENCODE

Hey there, I have a JSON object saved as a string in DB and it is encoded and thus properly escaped.

If i need to change one value within the JSON object, i am currently pulling the whole thing out, running through an online JSON_DECODER and then re-shrinking and re-saving in the DB.

I know this is not optimal for now, but I'm still in DEV in this just seems easiest.

My question is:

Is there a simple online tool anywhere that will re json_encode the data properly escaped?

I haven't found anything yet to do this. What I've been doing is viewing the json_encoded data in the browser and copy and pasting back into the DB.

Secondly, is it actually much better to be escaped?

Thanks

0 likes
2 replies
Cronix's avatar

i am currently pulling the whole thing out, running through an online JSON_DECODER and then re-shrinking and re-saving in the DB.

Why? I mean, why not just use php to json_decode() back to a php array, do your manipulation, and then json_encode() it back and save it?

$model = SomeModel::find(1);

// decode json back to associative php array
$data = json_decode($model->columnWithJson, true);

// change a property
$data['field'] = $newValue;

// encode php array as json
$data = json_encode($data);

// save
$model->update(['columnWithJson' => $data]);

There are easier ways to do this (setting accessors/mutators), but this is the gist.

https://laravel.com/docs/5.7/eloquent-mutators#attribute-casting

http://php.net/manual/en/function.json-decode.php

http://php.net/manual/en/function.json-encode.php

2 likes
futurefuture's avatar

@cronix the reason I am doing it all online is because I want other people on the team to be able to easily edit it.

I guess I could just create a form input that would decode and encode upon save.

just found it strange that there is not an online tool for this.

Thanks!

Please or to participate in this conversation.