5

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 =)

John Machin
  • 81,303
  • 11
  • 141
  • 189
  • 1
    What does `mb_strlen((string)$results, 'utf-8')` give you? – Emil Vikström Jun 12 '11 at 21:04
  • 1
    Maybe some whitespace is returned together with the JSON? Try trimming it, also can you add the returned JSON before the decode to you post please? – Mick Hansen Jun 12 '11 at 21:06
  • 1
    Also, `var_dump($results)` is more useful then printing its contents. – Matthew Jun 12 '11 at 21:09
  • Also echo it to the response, and the view source, rather than just viewing whats rendered in your browser. Also check mb_strlen() vs strlen() incase the JSON contains multibyte characters. – carpii Jun 12 '11 at 21:12

4 Answers4

17

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.

Seldaek
  • 40,986
  • 9
  • 97
  • 77
  • 5
    Dude, I love you. htmlentities() show me that the data returned have "&quotes" as quotes in the middle of the string. Just made html_entity_decode((string)$results) and everything works wonderful. Thank you very much! – Francisco Gutiérrez Jun 12 '11 at 21:36
  • You're welcome. Since you're new here let me point out that it's nice to accept the answer if it helped :) – Seldaek Jun 12 '11 at 21:44
  • 1
    I'd also advise that you should "View Source" next time. – Christian Jun 13 '11 at 00:05
  • As of PHP 5.5.0, you can also use `json_last_error_msg` to print the human-readable form of the error. :-) Check the docs: http://php.net/manual/en/function.json-last-error-msg.php –  Sep 09 '15 at 09:16
1

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" }
tparton42
  • 31
  • 3
1

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.

Alp Altunel
  • 3,324
  • 1
  • 26
  • 27
0

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.

Matija
  • 17,604
  • 2
  • 48
  • 43