# Help with JSONified objects from ActiveRecord

**URL:** <https://www.ruby-forum.com/t/help-with-jsonified-objects-from-activerecord/127534>\
**Category:** Rails\
**Created:** [January 30, 2008, 4:54am UTC](https://www.ruby-forum.com/t/help-with-jsonified-objects-from-activerecord/127534 "2008-01-30T04:54:59Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![lobo](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/l/76d3ee/32.png) [@lobo](https://www.ruby-forum.com/u/lobo)\
**Post date:** [January 30, 2008, 4:54am UTC](https://www.ruby-forum.com/t/help-with-jsonified-objects-from-activerecord/127534/1 "2008-01-30T04:54:59Z")

</div>

Hello guys,

I’m trying to return a custom JSON response to a client frontend.  
The object I’m trying to return is a User, with some associated Locale  
data.

So basically I need to put the User data and his Locale in a “data”  
property in the JSON response.

If this worked, it would be perfect:  
json = Hash.new  
json[:success] = false  
json[:data] = User.find(params[:id], :include =\> :locale)  
json[:success] = true  
render :json =\> json

The example above doesn’t include the association in the returned data  
(I know that include is used so ActiveRecord won’t query the DB again in  
case we use the locale data).  
What would I need to do to return the associated Locale data with the  
User, I would like to use the approach of using a Hash, like in the  
example above.

I know you can do something like this:  
json = User.find(params[:id]).to\_json(:include =\> :locale)

But that stringify the record, and I don’t see an easy way to put that  
data into a JSON property and do something like this:  
json = Hash.new  
json[:success] = false  
json[:data] = User.find(params[:id]).to\_json(:include =\> :locale)  
json[:success] = true  
render :json =\> json

That above doesn’t work, because the to\_json method already put a JSON  
string in json[:data].

Any suggestions?

Thanks in advance.
