Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to edit specific JSON key values using PHP

Tags:

json

php

I have a JSON file with a JSON object I am attempting to read and edit using PHP but I wanted to change specific Key values which is proving to be abit of a problem. Would anyone have any pointers or links they may know that may help?

Thanks

like image 200
user5898266 Avatar asked Jun 13 '26 08:06

user5898266


1 Answers

You can try this.

Firstly, decode your JSON:

$json_object = file_get_contents('some_file_name.json');
$data = json_decode($json_object, true);

Then edit what you want such as:

$data['some_key'] = "some_value";

Finally rewrite it back on the file (or a newer one):

$json_object = json_encode($data);
file_put_contents('some_file_name.json', $json_object);

Note: I assumed that the JSON comes from a file, but in place of that file system function you can very well use anything that returns a JSON object.

like image 162
shogitai Avatar answered Jun 15 '26 20:06

shogitai



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!