Brannon
Brannon

Reputation: 1336

json_decode behaving unpredictably

I am building a public API in php and am having a very strange issue with json_decode(). I am bringing in a large string of JSON that I am dynamically forming and having much trouble in the process. I am attempting to load the JSON object in like this:

$all_related_users_obj = json_decode($this->get_related_users_raw_JSON($user_id));
if(is_object($all_related_users_obj)) echo $all_related_users_obj->tags[0]->first_name;
else echo "NOT AN OBJECT";

I am finding that this works for about half of the $user_id integers that I pass it but not for the rest. Normally I would assume that my JSON was simply malformed but according to JSONlint all of my JSON is properly formed.

I created a gist of an example of a JSON file that correctly loads into $all_related_users_obj and one that does not.

Using the json_last_error() function I am receiving the JSON_ERROR_UTF8 stating that the JSON contains Malformed UTF-8 characters, possibly incorrectly encoded. I still, however, cannot find the errors.

Your help is much appreciated.

Upvotes: 0

Views: 114

Answers (1)

user149341
user149341

Reputation:

In your "not working" example, one of the keys buried deep in the JSON is:

"city":"Rezé",

This contains the non-ASCII character é. Chances are that the JSON is being submitted as a non-UTF8 encoding (probably ISO8859-1), which is making json_decode() barf.

Upvotes: 3

Related Questions