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);
}
});