Here is my JSON
[ { "activity_code":"1", "activity_name":"FOOTBALL" }, { "activity_code":"2", "activity_name":"CRICKET" } ]
I need to update {"activity_code":"1","activity_name":"FOOTBALL"}
to {"activity_code":"1","activity_name":"TENNIS"}
based on activity_code
How can I achieve this in PHP?
Procedure. In the Enterprise Explorer view, right-click your . json file or other file type that contains JSON code and select Open With > JSON Editor. You can compress JSON strings so that the strings display on one line with white space removed between JSON elements.
You can use json_mergepatch in an UPDATE statement, to update the documents in a JSON column.
First, you need to decode it :
$jsonString = file_get_contents('jsonFile.json'); $data = json_decode($jsonString, true);
Then change the data :
$data[0]['activity_name'] = "TENNIS"; // or if you want to change all entries with activity_code "1" foreach ($data as $key => $entry) { if ($entry['activity_code'] == '1') { $data[$key]['activity_name'] = "TENNIS"; } }
Then re-encode it and save it back in the file:
$newJsonString = json_encode($data); file_put_contents('jsonFile.json', $newJsonString);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With