I'm trying so get the number of items in the array inside this piece
JSON
{
"collection" : [
{
"item": "apple"
},
{
"item": "banana"
}]
}
Using the following JS (NodeJS): Updated with answers from user 'elssar'
var data = JSON.parse(fs.readFileSync(filePath));
console.log(data.collection.length);
Expected result: 2
Without specifying the encoding data
will be a buffer instead of string (thanks to user nils). JSON.parse should work for both. Now I'm getting an error Unexpected token ? at Object.parse (native)
. Any idea how to fix this? (using Node 5.2.0)