title: How to create complex Json response in ruby in rails
body: <p>I am working on ruby on rails project and I want to add respond to Json.</p>
  
  <p>One simple way is:--</p>
  
  <pre><code>def index
    @users = User.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml =&gt; @users }
      format.json  { render :json =&gt; @users.to_json }
    end
  end
  </code></pre>
  
  <p>But there are some issues with this:-</p>
  
  <ol>
  <li>I don't want to give the whole user object in json response like password hash and cache counter attributes. Facebook, twitter attributes etc.</li>
  <li>I want to add more details in the json object (considering stackoverflow model) like Latest question by each user, latest answer given by each user. Instead of image name stored in db via paperclip, I want to pass full url of the image. </li>
  </ol>
  
  <p>So the question is how can code json reponse in index.json.erb file like we do in index.html.erb. Format your own json response as per needs.</p>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>def index
    @users = User.all
    respond_to do |format|
      format.html # index.html.erb
      format.xml  { render :xml =&gt; @users }
      format.json  { render :file =&gt; "index.json.erb", :content_type =&gt; 'application/json' }
    end
  end
  </code></pre>
  
  <p>index.json.erb file:-</p>
  
  <pre><code>    &lt;% @users.each do |user| %&gt;
      {
        first_name: &lt;%= user.first_name %&gt;,
        last_name: &lt;%= user.last_name %&gt;  
      }
  
      &lt;% end %&gt;
  </code></pre>
  
  <p>Error:--</p>
  
  <p>template missing.</p>
  
  <p>PS:-- I am just trying using creating this file. This is just a sample</p>
  
  <p><strong>EDIT</strong></p>
  
  <p><img src="https://i.stack.imgur.com/qzTK4.png" alt="enter image description here"></p>
  
  <p><strong>edit</strong></p>
  
  <pre><code> { { first_name: Mohit , last_name: Jain } { first_name: Sahil Miglani, last_name: } { first_name: Hitesh Bansal, last_name: } { first_name: Sudhanshu, last_name: } { first_name: Saakshi, last_name: } { first_name: Kutta, last_name: } { first_name: bc, last_name: } { first_name: hey, last_name: } { first_name: fewhjfbwfb, last_name: vdsv } } 
  </code></pre>
  
  <p><strong>EDIT</strong></p>
  
  <pre><code>    [ { first_name: Mohit , last_name: Jain } , { first_name: Sahil Miglani, last_name: } , { first_name: Hitesh Bansal, last_name: } , { first_name: Sudhanshu, last_name: } , { first_name: Saakshi, last_name: } , { first_name: Kutta, last_name: } , { first_name: bc, last_name: } , { first_name: hey, last_name: } , { first_name: fewhjfbwfb, last_name: vdsv } ] 
  </code></pre>
  
  <p>EDIT, quite close to find out the solution:-</p>
  
  <pre><code>    [
      &lt;% @users.each_with_index do |user,index| %&gt;
      {
          &lt;%= "first_name".to_json.html_safe %&gt;: &lt;%= user.first_name.to_json.html_safe %&gt;,
          &lt;%= "last_name".to_json.html_safe %&gt;: &lt;%= user.last_name.to_json.html_safe %&gt;  
      }
      &lt;% unless index== @users.count - 1%&gt;
      ,
      &lt;% end %&gt;
      &lt;% end %&gt;
      ]
  </code></pre>
  
  <p>Response:-</p>
  
  <pre><code>    [
  
         -
         {
             first_name: "Mohit "
             last_name: "Jain"
         }
         -
         {
             first_name: "Sahil Miglani"
             last_name: null
         }
         -
         {
             first_name: "Hitesh Bansal"
             last_name: null
         }
         -
         {
             first_name: "Sudhanshu"
             last_name: null
         }
         -
         {
             first_name: "Saakshi"
             last_name: null
         }
         -
         {
             first_name: "Kutta"
             last_name: null
         }
         -
         {
             first_name: "bc"
             last_name: null
         }
         -
         {
             first_name: "hey"
             last_name: null
         }
         -
         {
             first_name: "fewhjfbwfb"
             last_name: "vdsv"
         }
  
      ]
  </code></pre>
  
  <p>Now all i want is to enclose this each response section in user array</p>