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

192
Visualizações
Reading data from external .json file in javascript and node.js using key and value

I am trying to work with a .json file and count how many times a value appears.

I have my example metadata.json file

{
  "name": "#1",
  "attributes": [
    {
      "trait_type": "Background",
      "value": "Neon"
    },
    {
      "trait_type": "Color",
      "value": "Red"
    }
  ],
},
{
  "name": "#2",
  "attributes": [
    {
      "trait_type": "Background",
      "value": "Neon"
    },
    {
      "trait_type": "Color",
      "value": "Red"
    }
  ],
}

I'm reading the .json file using the following code, which may not be the best way but does return the contents when I console.log. What I am having trouble with is how to return the value of a specific trait, such as 'Neon' from 'Background'. More specifically, I would like to count how many times 'Neon' appears in the .json file.

const fs = require("fs");
const path = require("path");
const isLocal = typeof process.pkg === "undefined";
const basePath = isLocal ? process.cwd() : path.dirname(process.execPath);


fs.readFile(`${basePath}/build/json/metadata.json`, 'utf8', function(err, contents) {
    if (err) {
      // we have a problem because the Error object was returned
      console.log('Error reading file')
    } else {
        // read json data
        let rawdata = fs.readFileSync(`${basePath}/build/json/metadata.json`);
        let data = JSON.parse(rawdata);
        console.log(data);
        }
    });
about 4 years ago · Juan Pablo Isaza
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