Problem with json_decode PHP

I'm receiving a json array from python as the return of curl_exec in PHP (first json PHP -> python, that returns another json), and decode fails due to bad syntax. The json received is valid, but somehow if I cast this json to string and prints it I get a string with 29 characters, but if I print strlen((string)$my_json) it says 50.

Here's the code:

$results = curl_exec($ch);
curl_close($ch);
var_dump(json_decode($results));

And that returns NULL. If I do the following

echo (string)$results;

It prints [[11, "stuffstuf", "stuffs"]] (29 chars), which is a valid json. But if I do

echo strlen((string)$results);

It prints 50.

So, I don't know what is going on. Any thoughts would be appreciated =)

Upvotes: 6

Views: 32975

Answers (4)

Alp Altunel
Alp Altunel

Reputation: 3443

its possible to print out json_last_error_msg() which gives error in text format not code. So no need to use switch and error handling.

Upvotes: 1

Seldaek
Seldaek

Reputation: 42026

Could it be that you have some html tags around it that you don't see when doing the simple echo?

Try: echo htmlentities((string)$results); to see more, or check the html source of the page.

If json_decode() fails, it means the string isn't standard JSON.

You can also use json_last_error_msg() to figure out why it returned NULL. That will return an error message if there was any error in json_decode.

Upvotes: 18

tparton42
tparton42

Reputation: 31

Seldaek's answer was a big help, and I believe it to be the overall best answer.

I have encountered a similar issue, caused by using single quotes instead of double quotes, and the differences between the latest supported version of PHP on Red Hat, versus PHP 5.5 on Ubuntu.

PHP on RHEL returned "Invalid or malformed JSON" when reading this next line in from a file, where as it was fine on my Ubuntu PHP 5.5 instance.

{ 'book': 'Dune', 'author': 'Frank Herbert', 'ISBN-13': '978-0441172719' }

Changing to double quotes, like below, resolved my issue

{ "book": "Dune", "author": "Frank Herbert", "ISBN-13": "978-0441172719" }

Upvotes: 1

Matija
Matija

Reputation: 17902

Seldaek said right, use of json_last_error is very good. I also use stripslashes before json_decode. Here is my code:

$resp = stripslashes($resp);

$resp_json = json_decode($resp);

switch(json_last_error())
{
    case JSON_ERROR_DEPTH:
        echo ' - Maximum stack depth exceeded';
    break;
    case JSON_ERROR_CTRL_CHAR:
        echo ' - Unexpected control character found';
    break;
    case JSON_ERROR_SYNTAX:
        echo ' - Syntax error, malformed JSON';
    break;
    case JSON_ERROR_STATE_MISMATCH:
        echo ' - Invalid or malformed JSON';
    break;
    case JSON_ERROR_UTF8:
        echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
    break;
}

After that when you debug and you still have let's say error 4 - JSON_ERROR_SYNTAX. get VALUE of variable $resp in debug mode and paste it to free web tool for JSON conversion @ Json conversion check - jsonlint. And check what's the deal with your conversion.

Upvotes: 1

Related Questions