Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

224
Views
JSON file getting passed as JavaScript object in Express Node.js?

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');
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

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

});
about 4 years ago · Juan Pablo Isaza Report

0

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);
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!