PHPLover
PHPLover

Reputation: 12957

Why the JSON data is not getting converted into an array in following case?

Following is my code :

$json_body = $application->request->getBody();
    /*echo "JSON Body : ".$json_body; 
    die;
prints following data :
JSON Body : 
{  “current_user_id”:901
   "user_id":990
}
*/

$request_data = json_decode($json_body, true); // Parse the JSON data to convert that into an assoc. array
print_r($request_data); die;//This statement prints nothing

I'm not getting why the array is notgetting printed after executing the statement $request_data = json_decode($json_body, true);

Please somebody help me.

Upvotes: 0

Views: 41

Answers (2)

MrRP
MrRP

Reputation: 822

Your JSON string is invalid. Missing the comma and not equals "

Valid JSON:

{
  "current_user_id": 901,
  "user_id": 990
}

Upvotes: 1

MASNathan
MASNathan

Reputation: 341

It looks like the $application->request->getBody(); method is returning an invalid JSON string

{  “current_user_id”:901
   "user_id":990
}

It's missing a comma ( , ) after the 901 value, should be something like this:

{
    "current_user_id":901,
    "user_id":990
}

Also, I'm not sure if it's related but the quotation marks used on “current_user_id” might not be supported: is diferent from "

Upvotes: 1

Related Questions