How do I prevent Nokogiri from adding unnecessary HTML tags?
<p>I’m using Rails 4.2.3.  I have this code that is meant to get the content of a URL</p>

<pre><code>  doc = Nokogiri::HTML(open(url))
</code></pre>

<p>Sometimes a URL will return HTML but sometimes it will return JSON.  I do not know in advance.  What I have noticed is that when the URL returns JSON, Nokgiri adds all these HTML tags in front of it.  Here is what appears in a browser:</p>

<pre><code>{"list":[{"u":"1459808276_000001","i":"1459184695_000001","pid":"RDE8UZZZ”,”fname":"Alexi","lname”:”Jones”,”sex":"F","city":"Eugene","country":"US","country_iso":"us","course":"8k","class":"elite","race":"8K","name":"Alexi Jones”,”_ver":"14","tag":"0000001","bib":"1"}],"info":{"first":"1","last":"1","total":"1","cacheVer":"0~0"}}
</code></pre>

<p>However when I execute Nokogiri, this is returned: </p>

<pre><code>&lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd"&gt;
&lt;html&gt;&lt;body&gt;&lt;p&gt;{"list":[{"u":"1459808276_000001","i":"1459184695_000001","pid":"RDE8UZZZ”,”fname":"Alexi","lname”:”Jones”,”sex":"F","city":"Eugene","country":"US","country_iso":"us","course":"8k","class":"elite","race":"8K","name":"Alexi Jones”,”_ver":"14","tag":"0000001","bib":"1"}],"info":{"first":"1","last":"1","total":"1","cacheVer":"0~0"}}&lt;/p&gt;&lt;/body&gt;&lt;/html&gt;
</code></pre>

<p>How do I prevent Nokogiri from adding extra stuff?  I just want it to return exactly what is returned to a browser.</p>

<p>When I try <code>doc = Nokogiri::HTML.fragment(open(url))</code> as recommend by another SO answer, I get the error:</p>

<pre><code>error: undefined method `strip' for #&lt;StringIO:0x007ff8acb34c30&gt;
</code></pre>