I was recently working on generating a JSON object using data from a database and ran into the problem that my “description” field was showing as null after I ran it through json_encode(). I checked the array prior to running it into json_encode() and it showed the description like it should. After pondering, testing, and mentally throwing my computer out the window… I wondered if the problems might be that there was a character json_encode() couldn’t handle. I didn’t spot anything when I was looking at the value in the database, so I turned to Google. I read some posts suggesting that it may be caused because of an encoding issues (the text was supposed to be encoded as UTF-8, but had been inserted with some invalid characters). The docs for json_encode say that the string being encoded must be UTF-8 to work.
If you need to clean a string before doing json_encode, this will ensure only valid UTF-8 characters are used:
iconv('UTF-8', 'UTF-8//IGNORE', $string);