Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

193
Vistas
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 la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda