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

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.
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!
Thanks for this simple but excellent help :-)
I had big headache this night. But now it works again :-)
God bless you!
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*
Muchas gracias amigo !
Thank you a lot, this fixed my site!
Thanks dude, after an hour googling you gave me the correct answer… :)
Yes! Thank you!! I owe you a beer next time I’m in Perth.
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!
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.
just a little modification :)
function _json_decode($string, $flag = false) {
if (get_magic_quotes_gpc()) {
$string = stripslashes($string);
}
return json_decode($string, $flag);
}
You da man! I don’t enjoy debugging AJAX, and this saved my butt.
Awesome! Thanks for preventing me from banging my head.
thanks dear, its really helpfull…
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!
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
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.