# to\_json() does not have behave like to\_xml builder and block support

**URL:** <https://discuss.rubyonrails.org/t/to-json-does-not-have-behave-like-to-xml-builder-and-block-support/36044>\
**Category:** rubyonrails-talk\
**Created:** [November 5, 2008, 2:49am UTC](https://discuss.rubyonrails.org/t/to-json-does-not-have-behave-like-to-xml-builder-and-block-support/36044 "2008-11-05T02:49:02Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![RubyNewbie](https://avatars.discourse-cdn.com/v4/letter/r/58f4c7/32.png) [@RubyNewbie](https://discuss.rubyonrails.org/u/RubyNewbie)\
**Post date:** [November 5, 2008, 2:49am UTC](https://discuss.rubyonrails.org/t/to-json-does-not-have-behave-like-to-xml-builder-and-block-support/36044/1 "2008-11-05T02:49:02Z")

</div>

Our Rails application needs to support both xml and json formats. In some cases, we need to return custom XML that spans several objects and associations. I've been able to write custom to\_xml methods that use the builder and block functionality. When trying the same thing with the to\_json method, I failed to get the same result.

Basically to\_xml has this signature: to\_xml(options = {}, &block). I've perused around and it appears to\_json has this signature: to\_json(options = {}). Is there a version of to\_json that will yield to a block and provide the json builder? Can someone please tell me if I've done something wrong with the code below? Is there is a plugin or newer version of Rails that will add the same support for to\_json that to\_xml supports?

Here is the relevant code

class Conference \< ActiveRecord::Base &nbsp;&nbsp;has\_many :courses, :dependent =\> :destroy end

class Course \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :conference &nbsp;&nbsp;has\_many :sessions, :dependent =\> :destroy &nbsp;&nbsp;has\_one :manual

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# just a place holder method so we can serialize it &nbsp;&nbsp;def pre\_requisite &nbsp;&nbsp;&nbsp;&nbsp;"An open mind" &nbsp;&nbsp;end end

class Session \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :courses &nbsp;&nbsp;has\_one :presentation, :dependent=\>:destroy

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;# just a place holder method so we can serialize it &nbsp;&nbsp;def maximum\_capacity\_reached? &nbsp;&nbsp;&nbsp;&nbsp;false; &nbsp;&nbsp;end end

class Presentation \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :session &nbsp;&nbsp;belongs\_to :instructor end

class Manual \< ActiveRecord::Base &nbsp;&nbsp;belongs\_to :course end

class Instructor \< ActiveRecord::Base end

Now let's suppose I have a controller that will return the full detail of a conference and needs all of the related data and will also serialize some custom methods. class ConferencesController \< ApplicationController &nbsp;&nbsp;# GET /conferences/:id &nbsp;&nbsp;def show &nbsp;&nbsp;&nbsp;&nbsp;conference\_id = params[:id]

&nbsp;&nbsp;&nbsp;&nbsp;respond\_to do |format| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.html # unsupported &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.json { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:json=\>Conference.full\_json(conference\_id)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;format.xml { &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;render(:xml=\>Conference.full\_xml(conference\_id)) &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end

So I've extended the conference class so that it can build this nested xml along with the result of selective methods on various classes. this code uses the fact that to\_xml has this signature: to\_xml(options = {}, &block)

class Conference \< ActiveRecord::Base &nbsp;&nbsp;has\_many :courses, :dependent =\> :destroy

&nbsp;&nbsp;def slogan &nbsp;&nbsp;&nbsp;&nbsp;"#{name}: Be here or be in the dark!" &nbsp;&nbsp;end

&nbsp;&nbsp;def self.full\_xml(conference\_id) &nbsp;&nbsp;&nbsp;&nbsp;# ensure we've loaded the full structure in the most efficient manner &nbsp;&nbsp;&nbsp;&nbsp;conference = Conference.find(conference\_id, :include=\>{:courses =\> [:manual, {:sessions=\>:presentation}]})

&nbsp;&nbsp;&nbsp;&nbsp;# extract detailed hierarchical xml &nbsp;&nbsp;&nbsp;&nbsp;conference.to\_xml do |xml| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xml.slogan conference.slogan # output the banner

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#iterate courses and any custom methods &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xml.courses{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conference.courses.each do |course| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;course.to\_xml(:builder=\>xml, :skip\_instruct =\> true) do | xml\_course| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xml\_course.sessions{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#iterate sessions and any custom methods &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;course.sessions.each do |session| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.to\_xml(:builder=\>xml\_course, :skip\_instruct =\> true) do |xml\_course\_session| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;xml\_course\_session.is\_at\_capacity session.maximum\_capacity\_reached? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end

&nbsp;&nbsp;def self.full\_json(conference\_id) &nbsp;&nbsp;&nbsp;&nbsp;# ensure we've loaded the full structure in the most efficient manner &nbsp;&nbsp;&nbsp;&nbsp;conference = Conference.find(conference\_id, :include=\>{:courses =\> [:manual, {:sessions=\>:presentation}]})

&nbsp;&nbsp;&nbsp;&nbsp;debugger()

&nbsp;&nbsp;&nbsp;&nbsp;# extract detailed hierarchical xml &nbsp;&nbsp;&nbsp;&nbsp;conference.to\_json do |json| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json.slogan conference.slogan # output the banner

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#iterate courses and any custom methods &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json.courses{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;conference.courses.each do |course| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;course.to\_json(:builder=\>json, :skip\_instruct =\> true) do | json\_course| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_course.sessions{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;#iterate sessions and any custom methods &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;course.sessions.each do |session| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;session.to\_json(:builder=\>json\_course, :skip\_instruct =\> true) do |json\_course\_session| &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;json\_course\_session.is\_at\_capacity session.maximum\_capacity\_reached? &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;end &nbsp;&nbsp;end end

When I invoke the method, via [http://localhost:3000/conferences/1.xml](http://localhost:3000/conferences/1.xml), I get the desired generated XML output: \<conference\> \<created-at type="datetime"\>2008-11-04T13:59:41-08:00\</created-at\> \<id type="integer"\>1\</id\> \<name\>Ruby Live\</name\> \<updated-at type="datetime"\>2008-11-04T13:59:41-08:00\</updated-at\> \<slogan\>Ruby Live: Be here or be in the dark!\</slogan\>

---

<div class="post-metadata">

**Author:** ![Frederick\_Cheung](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/frederick_cheung/32/3901_2.png) [@Frederick\_Cheung](https://discuss.rubyonrails.org/u/Frederick_Cheung)\
**Post date:** [November 5, 2008, 8:54am UTC](https://discuss.rubyonrails.org/t/to-json-does-not-have-behave-like-to-xml-builder-and-block-support/36044/2 "2008-11-05T08:54:24Z")

</div>

I don't believe there is a json builder - it's just not how the json stuff in rails works. basically there's an obvious implementation of to\_json for hashes and arrays, everything else just hangs off that (so to\_json on an activerecord object basically takes the hash of attributes (module except/only etc... options) and calls to\_json on it)

Fred
