# Json formatting

**URL:** <https://www.ruby-forum.com/t/json-formatting/167303>\
**Category:** Rails\
**Created:** [May 6, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/json-formatting/167303 "2009-05-06T16:14:20Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![lyntaff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/87869e/32.png) [@lyntaff](https://www.ruby-forum.com/u/lyntaff)\
**Post date:** [May 6, 2009, 4:14pm UTC](https://www.ruby-forum.com/t/json-formatting/167303/1 "2009-05-06T16:14:20Z")

</div>

I’m really new to rendering formats with rails and would appreciate a  
little help with a json issue.

I have created a booking model in which I would like to sync with a  
jquery calendar called “full calendar” through json. They have an  
example json script output with the following:

[  
{  
“id”:1,  
“title”:“Event1”,  
“start”:“2009-05-10”,  
},  
{  
“id”:2,  
“title”:“Event2”,  
“start”:“2009-05-20”,  
“end”:“2009-05-22”,  
“url”:“http://yahoo.com/”  
}  
]

When using the json format in rails I get:

[  
{  
“booking”:{  
“updated\_at”:“2009-05-01T15:42:44Z”,  
“title”:“Booking”,  
“property\_id”:1,  
“id”:1,  
“end”:“2009-05-19”,  
“start”:“2009-05-06”,  
“created\_at”:“2009-05-01T15:42:44Z”  
}  
},  
{  
“booking”:{  
“updated\_at”:“2009-05-06T09:09:48Z”,  
“title”:“Booking”,  
“property\_id”:1,  
“id”:2,  
“end”:“2009-08-26”,  
“start”:“2009-08-19”,  
“created\_at”:“2009-05-06T09:09:48Z”  
}  
}  
]

How do I change how the json format is rendered within rails 2. I need  
to get rid of “booking”, “updated\_at”, property\_id" and created\_at".

Thanks for any help.

---

<div class="post-metadata">

**Author:** ![lyntaff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/87869e/32.png) [@lyntaff](https://www.ruby-forum.com/u/lyntaff)\
**Post date:** [May 6, 2009, 10:14pm UTC](https://www.ruby-forum.com/t/json-formatting/167303/2 "2009-05-06T22:14:53Z")

</div>

You can avoid getting the table name - which I take it is “booking” in  
your case - by telling ActiveRecord not to include that with the  
following code

ActiveRecord::Base.include\_root\_in\_json = false

You can filter the fields you want in your database query and then  
to\_json on the query result should give you what you need without the  
table name.

-S

---

<div class="post-metadata">

**Author:** ![lyntaff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/87869e/32.png) [@lyntaff](https://www.ruby-forum.com/u/lyntaff)\
**Post date:** [May 8, 2009, 12:24am UTC](https://www.ruby-forum.com/t/json-formatting/167303/3 "2009-05-08T00:24:37Z")

</div>

you can use except option in to\_json  
Example :  
@booking.to\_json(:except=\>[:updated\_at,:property\_id,:created\_at]  
and to get rid of booking. From rails 2.1+ you should configure in  
config/initializers/new\_rails\_defaults.rb file :  
ActiveRecord::Base.include\_root\_in\_json = false

---

<div class="post-metadata">

**Author:** ![lyntaff](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/87869e/32.png) [@lyntaff](https://www.ruby-forum.com/u/lyntaff)\
**Post date:** [May 6, 2009, 9:54pm UTC](https://www.ruby-forum.com/t/json-formatting/167303/4 "2009-05-06T21:54:09Z")

</div>

I think you can do something like this…

@booking.to\_json(:only=\>[“title”,“id”,“start”,“end”,“url”])
