json_decode() Not Working in PHP?
Posted in Code, Tips and Tricks
– Check out some of our more recent Ruby on Rails blog posts. If you’d like to hire our team, get in touch –
This took me a few minutes today to work out and find an answer on Google so I thought I’d share it.
I had a string that I was posting from a form and while Firebug on the client said that the string was valid, on the server side PHP’s json_decode() function was returning me a NULL indicating invalid JSON. I spit out the string using good old var_dump() and I could see straight away that I would need to strip some slashes from it, but I couldn’t figure out why.
I don’t like to simply know how to fix something, I like to know why it was broken in the first place! A bit of Googling gave me the answer, it turned out to be because of the magic_quotes setting in PHP.
Instead of just wrapping everything in a stripslashes() call I decided to write a wrapper function that will take into account whether magic_quotes is on or off. It will also allow me to do whatever I want later one if I wanted to parse a particular type of data or something.
Here is the code. As you can see it’s nothing fancy, but it works and hopefully it saves people some time.
function _json_decode($string) {
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return json_decode($string);
}
Declan
Dec 31, 2008
Excellent – this has saved me some time. I was having trouble working out why a json_encoded object could not then be immediately json_decoded.
Manny Calavera
Feb 4, 2009
You are the MAN! I’ve spent a lot of time trying to figure out why json_decode was working great on my local server but once moved to my webserver it kept coming out NULL.
Thank you!
Miklós Kriván
Feb 10, 2009
Thanks for this simple but excellent help :-)
I had big headache this night. But now it works again :-)
God bless you!
Henrik Petersson
Feb 20, 2009
Magic quotes are evil, can’t imagine why anyone would leave it on by default.
Thanks for the info. I switched PHP distribution (to entropy) for my local server and couldn’t figure out why json_encode stopped working. Turned out magic quotes was turned on. *shudders*
JM
Feb 23, 2009
Muchas gracias amigo !
tim
Mar 19, 2009
Thank you a lot, this fixed my site!
Jaime
Jun 23, 2009
Thanks dude, after an hour googling you gave me the correct answer… :)
menslow
Jul 24, 2009
Yes! Thank you!! I owe you a beer next time I’m in Perth.
Arthur Kay
Aug 24, 2009
This has taken me hours to figure out, and I’ve utter more swear words at this damn thing than I care to admit.
Problem solved. Thanks for posting this!
Gordon
Jan 10, 2010
Hi, Thanks for this post. I’ve been scratching my head for the afternoon on this one, a piece of code using json_decode() was working on my local server but not when I put it online.
Greg
Jan 11, 2010
just a little modification :)
function _json_decode($string, $flag = false) {
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return json_decode($string, $flag);
}
Mark
Feb 26, 2010
You da man! I don’t enjoy debugging AJAX, and this saved my butt.
Justin
Mar 8, 2010
Awesome! Thanks for preventing me from banging my head.
kulish
Mar 11, 2010
thanks dear, its really helpfull…
James Beaule
Mar 24, 2010
You wouldnt believe how long ive been searching for something like this. Browsed through 5 pages of Yahoo results couldnt find diddly squat. Quick search on bing. There this is…. Really have to start using it more often!
Mazeb
Apr 24, 2010
Thx a lot man that was giving me a headache i knew it was something related to the server but couldn’t figure out what. And now it works like a charm
Kashish
Jun 30, 2010
Hey,
Thank you very very much.I was very stuck up with that problem as I am new to php.Searched a lot but coudn’t make out.Your code helped me a lot.Thank you once again.
virtualme123
Aug 13, 2010
Thanks Aaron, this was a great help. Was racking my brain for a while on this issue.
ZIOM
Sep 14, 2010
DUDE>< THANKS< LOVE U DAAAAMN
David
Oct 10, 2010
Thanks a lot man, it really helps
Joost
Oct 12, 2010
Thanks! Helped me out today :-)
gurunathan
Jan 24, 2011
$notes = json_decode($data);
$handle = fopen(‘patients/in_file.txt’, ‘r’);
$notes1 = json_decode(fgets($handle));
fclose($handle);
both fgets($handle) and $data has the same value
[{"ID":1,"LEFT":"70.87087087087087","TOP":"22.8","WIDTH":"4.804804804804805","HEIGHT":"3.2","DATE":{"Y":"2011","M":"01","D":"23","H":"22","I":"42"},"NOTE":"dsfs","AUTHOR":"","LINK":"","COLOR":"orange"}]
but $notes return NULL, where $notes1 returns Array ( [0] => stdClass Object ( [ID] => 1 [LEFT] => 70.87087087087087 [TOP] => 22.8 [WIDTH] => 4.804804804804805 [HEIGHT] => 3.2 [DATE] => stdClass Object ( [Y] => 2011 [M] => 01 [D] => 23 [H] => 22 [I] => 42 ) [NOTE] => dsfs [AUTHOR] => [LINK] => [COLOR] => orange ) ).
Please do help..
I tried your solution. But the issue not solved.
Steve
Feb 22, 2011
Thanks very very much,
Finding this solution has saved me from hours of frustration!!
Thanks Again!!!
same
Jun 28, 2011
same here. stupid magic quotes.
AHUOO
Sep 28, 2011
cool, thanks for your sharing.
Khayer
Oct 10, 2011
It save me from my client bullet !!!!! Than you very much..
Rachana
Dec 15, 2011
Hey mate! Thanks for sharing. This help me a lot.