6

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
83.3k12 gold badges147 silver badges193 bronze badges
asked Jun 12, 2011 at 21:00
4
  • 1
    What does mb_strlen((string)$results, 'utf-8') give you? Commented Jun 12, 2011 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? Commented Jun 12, 2011 at 21:06
  • 1
    Also, var_dump($results) is more useful then printing its contents. Commented Jun 12, 2011 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. Commented Jun 12, 2011 at 21:12

4 Answers 4

18

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.

answered Jun 12, 2011 at 21:05
Sign up to request clarification or add additional context in comments.

4 Comments

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!
You're welcome. Since you're new here let me point out that it's nice to accept the answer if it helped :)
I'd also advise that you should "View Source" next time.
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: php.net/manual/en/function.json-last-error-msg.php
1

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.

answered Aug 29, 2013 at 18:28

Comments

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" }
answered Apr 1, 2014 at 19:21

Comments

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.

answered Oct 31, 2015 at 14:52

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.