Take the 2-minute tour ×

I try to manually build a Json string to send to the client.


When I use

echo json_encode(array('result'=>'hhh'));

It arrives perfectly. But when I do

echo "{'result':'hhh'}";

It isn't

The only difference I find between the two requests is that the first one has:

Content-Length: 9    header

and the second one (which does not work)

Content-Length: 16   header

Both the strings should have been content length: 16!!! I guess it something to do with the combination of ZF and Mootools.

share|improve this question

2 Answers 2

up vote 2 down vote accepted

According to the specs, JSON requires double quotes around key names and string values.


will output


The length of this output is 16 bytes as shown by the following:

echo strlen(json_encode(array('result'=>'hhh')));

outputs "16".

Any JSON decoder that follows the specs will fail or throw an exception when presented with your manually echoed JSON.

share|improve this answer
If you will count the characters on my manually string, you will see the problem is something else completely. I need to investigate more, it seems something to do with the combination of ZF and Mootools –  Itay Moav -Malimovka Mar 26 '09 at 12:20
Just did some testing and Elmo is correct. You have to use double quotes, although I did not get an error trying to decode single quotes it was returning nothing. –  Erling Thorkildsen Mar 27 '09 at 21:21

It doesn't have problems with UTF-8 so much as UTF-8 is the standard encoding for it. It sounds as if you're echoing something in a different encoding scheme, which breaks, whereas json_encode() is transcoding it for you.

share|improve this answer

Your Answer


