How to edit specific JSON key values using 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

Here is Solutions:

We have many solutions to this problem, But we recommend you to use the first solution because it is tested & true solution that will 100% work for you.

Solution 1

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.

Solution 2

If you have a nested key, you can do something like this:

1. Decode JSON to PHP array

$arrayData = json_decode($jsonData, true);

2. Specify replacements recursively

$replacementData = array('a' => array('b' => 'random2'), 'c' => 'abc');

As an example this will replace the value of key b inside key a with random2 and the value of key c in root level with value abc .

3. Perform replacement recursively

$newArrayData = array_replace_recursive($arrayData, $replacementData);

4. Encode new JSON

$newJsonData = json_encode($newArrayData);

Test code

echo print_r(array_replace_recursive(array('a' => array('b' => 'random'), 'c' => 'def'), array('a' => array('b' => 'random2'), 'c' => 'abc')), true);

Should replace b inside a value random with random2 and c value def with abc and output:

Array(
    [a] => Array
        (
            [b] => random2
        )

    [c] => abc
)

Solution 3

Transforming json to arrays and recursively updating keys (depth-nth)

    function json_overwrite($json_original, $json_overwrite)
    {
        $original = json_decode($json_original, true);
        $overwrite = json_decode($json_overwrite, true);

        $original = array_overwrite($original, $overwrite);

        return json_encode($original);
    }

Recursively iterate and substitute $original

    function array_overwrite(&$original, $overwrite)
    {

        // Not included in function signature so we can return silently if not an array
        if (!is_array($overwrite)) {
            return;
        }
        if (!is_array($original)) {
            $original = $overwrite;
        }
        foreach($overwrite as $key => $value) {
            if (array_key_exists($key, $original) && is_array($value)) {
                array_overwrite($original[$key], $overwrite[$key]);
            } else {
                $original[$key] = $value;
            }
        }


        return $original;
    }

Quick test

    $json1 = '{"hello": 1, "hello2": {"come": "here!", "you": 2} }';


    $json2 = '{"hello": 2, "hello2": {"come": "here!", "you": 3} }';



    var_dump(json_overwrite($json1, $json2));

Note: Use and implement solution 1 because this method fully tested our system.
Thank you 🙂

All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply