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

191
Views
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
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!