Read json file content with require vs fs.readFile

2020-01-30 07:00发布

Suppose that for every response from an API, i need to map the value from the response to an existing json file in my web application and display the value from the json. What are the better approach in this case to read the json file? require or fs.readfile. Note that there might be thousands of request comes in at a same time.

Note that I do not expect there is any changes to the file during runtime.

request(options, function(error, response, body) {
   // compare response identifier value with json file in node
   // if identifier value exist in the json file
   // return the corresponding value in json file instead
});

6条回答
贼婆χ
2楼-- · 2020-01-30 07:09

There are two versions for fs.readFile, and they are

Asynchronous version

require('fs').readFile('path/test.json', 'utf8', function (err, data) {
    if (err) 
       // error handling

    var obj = JSON.parse(data);
});

Synchronous version

var json = JSON.parse(require('fs').readFileSync('path/test.json', 'utf8'));

To use require to parse json file as below

var json = require('path/test.json');

But, note that

  • require is synchronous and only reads the file once, following calls return the result from cache

  • If your file does not have a .json extension, require will not treat the contents of the file as JSON.

查看更多
冷血范
3楼-- · 2020-01-30 07:16

Use node-fixtures if dealing with JSON fixtures in your tests.

The project will look for a directory named fixtures which must be child of your test directory in order to load all the fixtures (*.js or *.json files):

// test/fixtures/users.json
{
  "dearwish": {
    "name": "David",
    "gender": "male"
  },
  "innaro": {
    "name": "Inna",
    "gender": "female"
  }
}
// test/users.test.js
var fx = require('node-fixtures');
fx.users.dearwish.name; // => "David" 
查看更多
▲ chillily
4楼-- · 2020-01-30 07:17

I suppose you'll JSON.parse the json file for the comparison, in that case, require is better because it'll parse the file right away and it's sync:

var obj = require('./myjson'); // no need to add the .json extension

If you have thousands of request using that file, require it once outside your request handler and that's it:

var myObj = require('./myjson');
request(options, function(error, response, body) {
   // myObj is accessible here and is a nice JavaScript object
   var value = myObj.someValue;

   // compare response identifier value with json file in node
   // if identifier value exist in the json file
   // return the corresponding value in json file instead
});
查看更多
女痞
5楼-- · 2020-01-30 07:25

If your file is empty, require will break. It will throw an error:

SyntaxError ... Unexpected end of JSON input.

With readFileSync/readFile you can deal with this:

let routesJson = JSON.parse(fs.readFileSync('./routes.json', 'UTF8') || '{}');

or:

let routesJson
fs.readFile('./dueNfe_routes.json', 'UTF8', (err, data) => {
    routesJson = JSON.parse(data || '{}');
});
查看更多
Explosion°爆炸
6楼-- · 2020-01-30 07:30

I only want to point out that it seems require keeps the file in memory even when the variables should be deleted. I had following case:

for (const file of fs.readdirSync('dir/contains/jsons')) {
  // this variable should be deleted after each loop
  // but actually not, perhaps because of "require"
  // it leads to "heap out of memory" error
  const json = require('dir/contains/jsons/' + file);
}

for (const file of fs.readdirSync('dir/contains/jsons')) {
  // this one with "readFileSync" works well
  const json = JSON.parse(fs.readFileSync('dir/contains/jsons/' + file));
}

The first loop with require can't read all JSON files because of "heap out of memory" error. The second loop with readFile works.

查看更多
一夜七次
7楼-- · 2020-01-30 07:32
{
  "country": [    
    "INDIA",
    "USA"
  ],
  "codes": [   
    "IN",
    "US"
  ]
}


//countryInfo.json

const country = require('countryInfo.json').country

const code = require('countryInfo.json').code

console.log(country[0])
console.log(code[0])
查看更多
登录 后发表回答