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

179
Visualizações
Read and edit specific value from json file in JS

I wanted to try to develop my own database with JS and Json. Unfortunately, I run into problems there.

How do I read specific content from the Json file I googled and got this:

fs.readFile('./databases/' + dbName + ".json", (err, data) => {
     if (err) throw err;
     content = JSON.parse(data);
     console.log(content)
});

But then I only get this output

{
  members: [
    { Username: 'User-1', Password: 'lol' },
    { Username: 'User-2', Password: 'lol' }
  ]
}

Process finished with exit code 0

and not a specific one, for example just the password of user-1 from the json file:

{
  "members": [
    {
      "Username": "User-1",
      "Password": "lol"
    },
    {
      "Username": "User-2",
      "Password": "lol"
    }
    ]
}

(I really only want to use json files for this project)

Thanks for helping :)

about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

your question does not have much informations


fs.readFile("./databases/" + dbName + ".json", (err, data) => {
  if (err) throw err;
  var username = "User-1";
  var password = "lol";

  for (var i = 0; i < content.members.length; i++) {
    if (
      content.members[i].Username == username &&
      content.members[i].Password == password
    ) {
      console.log("Login Successful");
      return true;
    }
  }
  console.log("Login Failed");
  return false;
});

get the username and password fields from user side and use the for loop to validate the values are in the json database file

about 4 years ago · Juan Pablo Isaza Relatório

0

As for storing data in a JSON-like object, databases already exist that offer great storage options out there for example MongoDB or Firebase.

As for your problem, the code is operating correctly what your looking for is a filter

fs.readFile('./databases/' + dbName + ".json", (err, data) => {
    if (err) throw err;

    const username = username;
    const password = password;

    //const username = "User-2";
    //const password = "lol";


    content = JSON.parse(data);
    console.log(content.members.filter(mem => { if (mem.Username === username && mem.Password === password) return mem} ));
});

This will return:

[ { Username: 'User-2', Password: 'lol' } ]

You'll have you include a way to accept the username and password or any other filter variables you can think of.

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