title: Rails filter JSON Object on render?
body: <p>I have a JSON string and I use:</p>
  
  <pre><code>objJson = JSON.parse(jsonString)
  
  respond_to do |format|
    format.html { render :json =&gt; objJson.to_json(:include =&gt;  [:ID, :Name]) }
  end
  </code></pre>
  
  <p>The problem is that <code>to_json</code> only works on models, so it just ignores my filters.</p>
  
  <p>Here is my JSON:</p>
  
  <pre><code>{
   "count":29,
   "results":
   [
     {"ID":556036109,"Name":"Mojos", "Url":"https://mojos/id300751949","currency":"USD"},
     {"ID":556036110,"Name":"Asav", "Url":"https://asav/id300751949","currency":"USD"}
   ]
  }
  </code></pre>
  
  <p>Is there any way to filter the JSON object on render?</p>
  
  <hr>
  
  <h2>UPDATE:</h2>
  
  <p>I used <em>ssorallen</em> code:</p>
  
  <pre><code>KEYS_TO_KEEP = ['ID', 'Name']
  objJson = JSON.parse(jsonString)
  
  respond_to do |format|
    format.html {render :json =&gt; objJson['results'].keep_if {|k, v| KEYS_TO_KEEP.include?(k)}}
  end
  </code></pre>
  
  <p>The problem is that objJson['results'] returned an Array to Hashes and not a single Hash.</p>
  
  <p>So the right way to do it was:</p>
  
  <pre><code>objJson = objJson['results'].each { |x| x.keep_if {|k, v| KEYS_TO_KEEP.include?(k)} }
  </code></pre>