Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

json_decode returns json string not an array

Tags:

json

ajax

php

<?php

$json=file_get_contents('php://input',true);
$data = json_decode($json, true);

print_r($data);
?>

Output given is {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}

Json Data posted is:

 {"EventTitle":"Game","EventBody":"body","EventDate":"20 November, 2016","EventType":"party"}

Writing the json data in a variable and passing it to json_decode works but posting the same from the "php://input" returns a JSON data instead of associative array.

like image 383
Nadir Laskar Avatar asked May 14 '26 05:05

Nadir Laskar


1 Answers

It looks like @tkausl is correct. The JSON you're receiving has been double-encoded. Since it's double-encoded, a temporary solution would be to double-decode it.

$data = json_decode(json_decode($json), true);

But the real solution is to figure out why it's like that to begin with and fix it (if it's yours to fix).

like image 128
Don't Panic Avatar answered May 15 '26 18:05

Don't Panic



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!