Skip to content

Instantly share code, notes, and snippets.

@jamesxv7
Created August 24, 2017 20:03
Show Gist options
  • Save jamesxv7/18b0cde3e343af8816df6b3e43efdfa8 to your computer and use it in GitHub Desktop.
Save jamesxv7/18b0cde3e343af8816df6b3e43efdfa8 to your computer and use it in GitHub Desktop.
Nested reading of a json structure using Node.js
const fs = require('fs');
var total = 0
fs.readFile('lists.json', 'utf8', function (err, data) {
if (err) throw err;
obj = JSON.parse(data);
for (var user in obj) {
console.log('Lists from', user)
for (var list in obj[user]) {
lists = obj[user]
for (var item in lists[list]) {
items = lists[list]
if (typeof (items[item]) === 'object') {
total += Object.keys(items[item]).length
}
}
}
console.log(total)
total = 0
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment