Today I was looking for a quick solution to outputting a response in JSON format from within one of my existing Zend_Controller actions that could be both requested and parsed using one of the jQuery Ajax methods.
The easiest method I could find was to use something similar to the following which first detects whether or not an XHTTP request has taken place, then outputs the response in plain text format.
This will output only the encoded array (similar to that below) without including the page template.
It is essential that you exit the script after outputting the JSON, otherwise your page template will be rendered as well as outputting the encoded array and therefore removing the ability for it to be parsed correctly.
It does however work as expected with Prototype, Scriptaculous, Yahoo! UI Library, jQuery and MochiKit.
There are many alternative methods that could be used instead of this one, it’s just that I found this one to be the quickest and most simplistic method.