JSON Returning NULL When Special Characters Exist in Result Set

Nearly 24 hours of my life lost and I just figured this out. Maybe I can save someone else some precious time.

Overview of JSON (skip this if you know what JSON is)

When sending information from JavaScript via AJAX to a PHP server, certain things don’t transfer because you are dealing with different programming languages. An example would be an array. A JS array is incompatible with a PHP array. The fix? JSON.

JSON is a standardized format that converts an array into a string value. This is called “stringify” in JavaScript.


I was struggling with certain entries in the database that returned “NULL”. Some worked, some returned NULL. What’s that about?

After looking into it I discovered that the character sets chosen are messing up JSON. In other words, odd HTML characters or encoded characters were getting lost in the JSON encoding and just being dropped. Every “solution” nearly solved my problem but left another issue remaining. I tried stripping slashes, encoding html_special_chars, decoding them, etc.


The only way I found to solve the problem was when inserting the data into the database. Simply convert the text using PHP’s utf8_encode() function, and voila!

$text = utf8_encode($text);

Insert the converted text and all is well.

1 Comment on JSON Returning NULL When Special Characters Exist in Result Set

  1. Thanks! I have spent nearly the same amount of time figuring this out. You made my day 🙂

This site uses Akismet to reduce spam. Learn how your comment data is processed.

%d bloggers like this: