I am using the following line to import a JSON file in my code. However, instead of a config file, the jsonConfig variable is getting a JavaScript object and I can directly access, jsonConfig.children. Why is this happening? And how can I just import a JSON file instead of the object?
const jsonConfig = require('../../config/myconfig.json');
That's what's supposed to happen using your method, if you want to access the contents of the file as a text you should use fs
fs.readFile('../../config/myconfig.json', function read(err, data) {
if (err) {
throw err;
}
const content = data;
// Invoke the next step here however you like
console.log(content); //Here you have the contents of your file
});
JSON is stand for Javascript Object Notation, so a JSON is a valid object in javascript for that reason you are getting an object, you can access to the values using object notation, if want to get a string you can use JSON.stringify to convert your object into a string.
const jsonConfig = require('../../config/myconfig.json');
const jsonString = JSON.stringify(jsonConfig);