The Frontier Group - Blog

json_decode() Not Working in PHP?

November 7th, 2008, by aaron

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

We are a web development company and this is our blog. We specialize in building web applications with the Ruby on Rails framework. You can read more about our Ruby on Rails development or contact us.

14 Responses to “json_decode() Not Working in PHP?”

  1. Declan says:

    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.

  2. Manny Calavera says:

    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!

  3. Thanks for this simple but excellent help :-)
    I had big headache this night. But now it works again :-)

    God bless you!

  4. Henrik Petersson says:

    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*

  5. JM says:

    Muchas gracias amigo !

  6. tim says:

    Thank you a lot, this fixed my site!

  7. Jaime says:

    Thanks dude, after an hour googling you gave me the correct answer… :)

  8. menslow says:

    Yes! Thank you!! I owe you a beer next time I’m in Perth.

  9. Arthur Kay says:

    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!

  10. Gordon says:

    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.

  11. Greg says:

    just a little modification :)

    function _json_decode($string, $flag = false) {
    if (get_magic_quotes_gpc()) {
    $string = stripslashes($string);
    }

    return json_decode($string, $flag);
    }

  12. Mark says:

    You da man! I don’t enjoy debugging AJAX, and this saved my butt.

  13. Justin says:

    Awesome! Thanks for preventing me from banging my head.

  14. kulish says:

    thanks dear, its really helpfull…

Leave a Reply


Follow Us

Stay in the Loop

  • Enter your email address to subscribe to our mailing list. You'll get updates about our products, specials and bonus offers, and general behind the scenes news from our team.

Twitter

Newsletters

Alexa Rank

Testimonial

The boys at The Frontier Group are amazing! For such a relaxed and personable organisation, they have phenomenal technical ability and a rampant professionalism. They have customisable solutions for all of my IT needs and they always deliver, on time and beyond expectation.

They fix problems other service providers can't and they helped me get a critical section of my web site up and running 10 minutes after I emailed the request!

Alex Hyndman, Nexus Car Share.

Featured Project

Case Study - Caudo Group - www.caudo.com.au

Website

www.caudo.com.au

Caudo Machinery

Caudo Group engaged our services to redesign their outdated website. We sent our photographer on-site to capture the essence of their business and turned it into a stunning web design.