Node JS Load JSON array from file
<p>a json file has been created with pretty print like that</p>

<pre><code>[
  {
    "name": "c",
    "content": 3,
    "_prototype": "item"
  },
  {
    "name": "d",
    "content": 4,
    "_prototype": "item"
  }
]
</code></pre>

<p>I can read the file with that</p>

<pre><code>var fs = require('fs');
var arrayPath = './array.json';

function fsReadFileSynchToArray (filePath) {
    var data = JSON.parse(fs.readFileSync(filePath));
    console.log(data);
    return data;
}

var test  = arr.loadFile(arrayPath);
console.log(test);
</code></pre>

<p>but the output is in reverse order</p>

<pre><code>[]
[ { name: 'c', content: 3, _prototype: 'item' },
  { name: 'd', content: 4, _prototype: 'item' },]
</code></pre>

<p>obviously the second output is shown as first. I used actually synchronous file read to avoid such an empty data stuff. Is there a way to really make sure the JSON file is completely read into an array before continuing ?</p>

<p>[update]
arr is a module which uses </p>

<pre><code>function loadFile(filePath){
    var arrLines = [];
    fs.stat(filePath, function(err, stat) {
        if(err == null) {
            arrLines = fsReadFileSynchToArray(filePath);
        } else if(err.code == 'ENOENT') {
            console.log('error: loading file ' + filePath + ' not found');
        } else {
            console.log('error: loading file', err.code);
        }
    });
    return arrLines;
}
</code></pre>

<p>to return the values</p>