Search code examples
phpjsonutf-8encode

Invalid PHP JSON encoding


I'm working on a project in PHP (5.3.1) where I need to send a JSON string to a webservice (in python), but the result I get from json_encode does not pass as a valid JSON (i'm using JSLint to check validity).

I should add that the structure I'm trying to encode is fairly big (13K encoded), and consists partially of UTF8 data, and while json_encode does handle it, i get spaces in weird places in the result. For example, I could get {"hello":tru e} or {"hell o":true} which results in an error from the webservice since the JSON is invalid (or data, like in the second example).

I've also tried to use Zend framework for JSON encoding, but that didn't make much different.

Is there a known issue with JSON in PHP? Did anyone encounter that behavior and found a solution?


Solution

  • I was handling some automatically generated emails the other day and noticed the same weird behavior (spaces were inserted to the email body), so I started to check the email post and found the culprit:

    From the SMTP RFC2821:

    The maximum total length of a text line including the is 1000 characters (not counting the leading dot duplicated for transparency).

    My email body was indeed in one line, so breaking it with \n's fixed the spaces issue.