views:

1384

answers:

4

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?

+1  A: 

For sure object keys cannot contain spaces or any non unicode characters, unquoted variables can by only boolean, integer ,float, object and array value, string always should be quoted. Also to add correct header before your json output.

if(!headers_sent()) header('Content-Type: application/json; charset=utf-8', true,200);

Can you also post your array or object that you passing to json_encode

Nazariy
Corrected "application/json," to "application/json;"
Greg
thanks Greg, fixed
Nazariy
A: 

You state that "the structure I'm trying to encode ... consists partially of UTF8 data." This implies that it is also partially of non-UTF8 data. The json_encode doc has a comment at the bottom, that

json_encode() expects strings to be encoded to be in UTF8 format, while by default PHP strings are ISO-8859-1 encoded. This means that

json_encode(array('àü'));

will produce a json representation of an empty string, while

json_encode(array(utf8_encode('àü')));

will work.

Are the failing segments of the JSON due to non-UTF8 input?

Ewan Todd
Actually, i did try to utf8 encode all my content prior to json encoding it, but that didn't work either
Ofir
The failures are not directly linked to UTF-8. I can get a boolean value with a space in it or something of that sort (tru e instead of true)
Ofir
+1  A: 

After scratching my head for nearly a day, I've come to the conclusion that the problem was not in the json_encode function. It was with my post function.

Basically, the json_encode was preparing the data to be sent to another service. Before today, I've used stream_context_create and fopen to post data to the external service, but now I use fsockopen and fputs and it seems to be working.

Although I'm unsure as to the nature of the problem, I'm happy it works now :)

BTW: After this process, I mail myself the input and output (both in JSON) and this is how I saw there was a problem in the first place. This problem still persists but I guess that's related to the encoding of the mail or something of that sort.

Ofir
A: 

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.

Ofir