8

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

1
  • 2
    Read, parse, modify, format, write. Which part do you have a problem with? Commented May 21, 2018 at 8:56

2 Answers 2

16

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.

Sign up to request clarification or add additional context in comments.

4 Comments

You may want to include how to encode the json again just incase OP doesn't know =)
Thanks. This really helped alot. The JSON file is in the same directory. Wanted to edit JSON object key values replacing them POST values
This isn't working for me. It's overwriting the JSON file with just the one key and value I am changing.
I Solved it. I had a function do the update while the JSON was loaded on the outside. The variables were out of scope, and since the JSON was hardcoded in the update function, it was just overwriting the entire file with only the key and value I updated
-1

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));

2 Comments

I would not look rude, but the user has specifically asked for a JSON that comes from a file and that fails to change a specified field. Your answer seems a little out of the question because it is based on the recursive modification of a JSON.
Title is Not asking how to read from file. It ask about editing keys. It is worth nothing key may stay at any depth level. And no specific key was mentioned. Please review carefully the question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.