-1

We know the difference between JSON and Object Literal from those two posts

What is the difference between JSON and Object Literal Notation? What's the difference between Javascript Object and JSON object

Today, I discuss this topic with my colleague about using JSON or Object Literal as config file in Node.js.

The opinion of my colleague is to use JSON.

// message.json
{   "aa": "content",
    "enable": true,
    "media": {
        "event": "Winner",
        "message": "Congratulations!"
    }
}

// app.js
filepath = "./message.json";
var file = fs.readFileSync(filepath, 'utf-8');
console.log(JSON.parse(file));

However, I insist on the Object Literal

// message.js
exports.value = {
    "aa": "content",
    "enable": true,
    "media": 
    {
        "event": "Winner",
        "message": "Congratulations!",
    }
}
//app.js
var vv = require('./message.js');

console.log(vv.value.media.message);

Could someone tell me the difference between them? Any one is better or not?

Community
  • 1
  • 1
zangw
  • 43,869
  • 19
  • 177
  • 214
  • 2
    Did you know that you can directly do: `var data = require('file.json');` and `require()` will parse the JSON for you? See http://stackoverflow.com/a/9804910/816620 – jfriend00 Aug 18 '15 at 04:33

5 Answers5

2

This is mostly opinion-based, but there's one main advantage to a JSON config - it's language-agnostic, so if you need a non-JS script to read the config for some other purpose, you can do so easily.

Using JSON also helps to ensure that your configuration is static, and removes the temptation to start adding logic and functions to it.

Note that you can also require('./message.json') as of Node > 0.5.x, so there's no real functional difference.

nrabinowitz
  • 55,314
  • 10
  • 149
  • 165
2

it boils down to your team's best practices, but i'd suggest using JSON.

  • the node.js convention is to use json for configuration files -- e.g., package.json being one example.

  • also, a serialization format like JSON can be handled by multiple systems, whereas it becomes awkard to do so with a JavaScript source file. your config file might be served from a configuration server at some point; a simple serialization will definitely be the way to go in such cases since the config server might ultimately be using a database to hold configuration information and simply prepares it as JSON on demand

user2524973
  • 1,087
  • 6
  • 14
0

it depends on personal preference but however i find exports way more convenient and kind of node way of requiring modules.

And i think in the back require is doing similar operation which you are doing in your first method, so why reinvent the wheel.

atinder
  • 2,080
  • 13
  • 15
0

My team uses JSON, but at least some hold your view that JS provides more flexibility.

https://blog.risingstack.com/node-js-best-practices-part-2/

aefhm
  • 541
  • 1
  • 5
  • 12
-1

I guess that the main difference is that, at the end of the day you will need a JavaScript object to work with. So if you are loading the config from a JSON file, there will be an extra step to parse the JSON string into an JS object.

Depending on the size of this JSON file, there might be a performance loss on parsing it.

ThiagoPXP
  • 5,362
  • 3
  • 31
  • 44