1

I'm calling this line in my browser:

example.com/save.php?params={"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}

and here's my save.php code:

<?php 
include 'Config.php';

    $getJSON = $_GET['params'];
    echo 'getJSON: '.$getJSON.'<br><br>';

    $updateArr = json_decode($getJSON, true);
    echo 'UPDATE_ARR: ' .$updateArr; //<-- it prints 'null'...

    $jsonStr = file_get_contents("Users.json");
    // Decode the JSON string into a PHP array.
    $objects = json_decode($jsonStr, true);

    array_push($objects, $updateArr);

    // Encode the array back into a JSON string and save it.
    $jsonData = json_encode($objects);
    file_put_contents("Users.json", $jsonData);

    // echo data
    echo 'JSON DATA: ' .$jsonData;
?>

The problem is this code makes my User.json file 'null', it removes all objects in it, in fact, the echo 'UPDATE_ARR: ' .$updateArr; returns null. I would need to decode my $getJSON string into a PHP array, as I do for my User.json file (the $jsonStr), but it doesn't work.

What am I doing wrong?

Thanks so much!

2
  • so what i pass after ‘params=‘ is not a String? Commented Sep 12, 2018 at 14:04
  • You can use json_last_error() to determine the exact nature of the error. php.net/manual/en/function.json-last-error.php Commented Sep 12, 2018 at 14:20

4 Answers 4

1

I have updated your code and tested at my side it's working now. Please try it.

  <?php 
    include 'Config.php'; 
     if(!empty($_GET['params'])){
    $getJSON = $_GET['params'];
    echo 'getJSON: '.$getJSON.'<br><br>';

    $updateArr = json_decode($getJSON, true);
    echo 'UPDATE_ARR: ';print_r($updateArr); //<-- it prints 'null'...

    $jsonStr = file_get_contents("Users.json");
    // Decode the JSON string into a PHP array.
    $objects = json_decode($jsonStr, true);

    array_push($objects, $updateArr);

    // Encode the array back into a JSON string and save it.
    $jsonData = json_encode($objects);
    file_put_contents("Users.json", $jsonData);

    // echo data
    echo 'JSON DATA: ' .$jsonData;
}else{
    echo 'Params is empty';
}

This is the outcome of users.json file after refreshing many times:

getJSON: {"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]} UPDATE_ARR: Array ( [objID] => i8O0FRuGEr [username] => johndoe [password] => 123456 [email] => [email protected] [followedBy] => Array ( [0] => john [1] => sarah ) ) JSON DATA: {"ID":"i8O0FRuGEr","user_name":"johndoe","pass":123456,"email_id":"[email protected]","followed_By":["john","sarah"],"0":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"1":null,"2":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"3":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"4":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"5":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"6":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"7":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},"8":{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}}

After using your users.json file:

[{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]},{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}]

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

12 Comments

oh, so it was a matter of wrong echoing :) thanks so much, it works now!
Yes, you were trying to echo array as a string .$updateArr;
i don’t know why but it still adds a null object in my User.json file.»
The same code as I updated? Let me check it again at my side.
Yes, it's adding null second time. The reason is that it adds null for duplicate data.
|
1

It seems $getJson is null.

check its value using var_dump function or print_r function.

var_dump($getJson); // see what is the output. 

Comments

1

this is no JSON string, while you won't properly urlencode() the query-string. die(urlencode('{"objID":"i8O0FRuGEr","username":"johndoe","password":123456,"email":"[email protected]","followedBy":["john","sarah"]}'));

^ append the above PHP output as params=.

rather proper would be, to POST the JSON as the content.

Comments

1

PHP documentation states that json_decode() returns NULL if the string cannot be decoded. Most likely it is a syntax error originating from bad url encoding.

Why are you json encoding url parameters in the first place? URL encoding is the accepted way to pass params in urls.

EDIT:

print_r($updateArr);

Shows a correct json, thus the problem is elsewhere

There are several other issues with your code.

  1. echo 'UPDATE_ARR: ' .$updateArr; throws Notice: Array to string conversion on line 6. Depending on your PHP version and configuration, your NULL could be printed because of bad typecasting in echo.
  2. Warning: array_push() expects parameter 1 to be array, null given on line 12. Check if your users.json file exists and is correct.

Comments

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.