JS read json file and use as an object
<p>I'm trying so get the number of items in the array inside this piece</p>

<p><strong>JSON</strong></p>

<pre><code>{
  "collection" : [
    {
      "item": "apple"
    },
    {
      "item": "banana"
    }]
}
</code></pre>

<p>Using the following <strong>JS</strong> <em>(NodeJS)</em>:
<em>Updated with answers from user 'elssar'</em></p>

<pre><code>var data = JSON.parse(fs.readFileSync(filePath));
console.log(data.collection.length);
</code></pre>

<p>Expected result: <strong>2</strong></p>

<p>Without specifying the encoding <code>data</code> will be a buffer instead of string (thanks to user nils). JSON.parse should work for both. Now I'm getting an error <code>Unexpected token ? at Object.parse (native)</code>. Any idea how to fix this? (using Node 5.2.0)</p>