# JSON render problem

**URL:** <https://www.ruby-forum.com/t/json-render-problem/182200>\
**Category:** Rails\
**Created:** [January 25, 2010, 3:24am UTC](https://www.ruby-forum.com/t/json-render-problem/182200 "2010-01-25T03:24:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![ang89](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@ang89](https://www.ruby-forum.com/u/ang89)\
**Post date:** [January 25, 2010, 3:24am UTC](https://www.ruby-forum.com/t/json-render-problem/182200/1 "2010-01-25T03:24:11Z")

</div>

Hi all,

In my controller, I have an action to render JSON string containing  
current user’s account information, except for his ID and hashed  
password. This is the snippet:

# Fetch the account information of the current user

def fetch\_data

record = User.find session[:user\_id]

render :json =\>  
{  
:success =\> true,  
:data =\> (record.to\_json(:except =\> [:id, :password]))  
}  
end

It does not work. The :data attribute contains a JSON string instead of  
an object.  
Sample output:

{“success”:true,“data”:"{“name”:“The  
Administrator”,“username”:“admin”,“role”:“admin”}"}

How can I fix this?

Thanks.

---

<div class="post-metadata">

**Author:** ![ang89](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@ang89](https://www.ruby-forum.com/u/ang89)\
**Post date:** [January 25, 2010, 3:51am UTC](https://www.ruby-forum.com/t/json-render-problem/182200/2 "2010-01-25T03:51:48Z")

</div>

-=

On Sun, Jan 24, 2010 at 9:24 PM, Andree S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> 

First, the above statement returns an instance of User,

> {“success”:true,“data”:“{"name":"The  
> Administrator","username":"admin","role":"admin"}”}
> 
> How can I fix this?

The above seems correct based on your render statement. What’s the  
problem?

-Conrad

---

<div class="post-metadata">

**Author:** ![ang89](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@ang89](https://www.ruby-forum.com/u/ang89)\
**Post date:** [January 25, 2010, 5:03am UTC](https://www.ruby-forum.com/t/json-render-problem/182200/3 "2010-01-25T05:03:46Z")

</div>

On Sun, Jan 24, 2010 at 10:10 PM, Andree S. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> Conrad T. wrote:
> 
> > The above seems correct based on your render statement. What’s the  
> > problem?
> > 
> > -Conrad
> 
> The data attribute should be an object, not a string.  
> Let’s compare the difference with the following snippet:

From the JSON specification, [http://www.json.org/](http://www.json.org/), an object in JSON  
consists of name/value pairs. The object here is “data” and you should  
be  
able to easily access  
this information on the client side.

Next, you can limit which fields get returned from a SQL query using  
‘find(:all, :select =\> “arg1, arg2, …”, … )’.

Finally, the easiest way to convert an AR instance to JSON is to use  
to\_json  
method on the instance (i.e. object.to\_json). Otherwise, you can write a  
simply method, to\_hash or to\_h, which returns the hash of the AR. Then  
you’ll have something like this:

def to\_hash  
{ :name =\> self.name, :username =\> self.username, :role =\> self.role }  
end

render :json =\> { :success =\> true, :data =\> record.to\_hash }

Good luck,

-Conrad

---

<div class="post-metadata">

**Author:** ![ang89](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/4af34b/32.png) [@ang89](https://www.ruby-forum.com/u/ang89)\
**Post date:** [January 25, 2010, 4:10am UTC](https://www.ruby-forum.com/t/json-render-problem/182200/4 "2010-01-25T04:10:49Z")

</div>

Conrad T. wrote:

> The above seems correct based on your render statement. What’s the  
> problem?
> 
> -Conrad

The data attribute should be an object, not a string.  
Let’s compare the difference with the following snippet:

render :json =\>  
{  
:success =\> true,  
:data =\> record # Without to\_json method  
}

The output will be: (the data attribute is an object)  
{“success”:true,“data”:{“name”:“The  
Administrator”,“username”:“admin”,“role”:“admin”,“id”:1,“password”:“d033e22ae348aeb5660fc2140aec35850c4da997”}}

Instead of: (the data attribute is a string)  
{“success”:true,“data”:"{“name”:“The  
Administrator”,“username”:“admin”,“role”:“admin”}"}

The problem is, I want to hide several attributes from the output, and  
the only method that I know is to use :except parameter from to\_json  
method.

So, my objective is to get the output similiar to output #1, but without  
the ID and password attribute.
