Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

225
Visualizações
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 Respostas
Responde à pergunta

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 Relatório

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 Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda