-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjsontab.js
More file actions
executable file
·48 lines (42 loc) · 951 Bytes
/
Copy pathjsontab.js
File metadata and controls
executable file
·48 lines (42 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#!/usr/bin/env node
/*
Prints every leaf value in JSON file on a single line prepended by all the
keys leading to it. Pipe through grep to find out which keys contain
matching values
*/
const fs = require('fs');
printTable();
function printTable() {
if(process.argv.length <= 2) {
console.error(`USAGE: ${process.argv[1]} JSON_FILE ...`);
process.exit(1);
}
for(let i = 2; i < process.argv.length; i++) {
printFile(process.argv[i]);
}
}
function printFile(path) {
fs.readFile(path, function(e, content) {
if(e) {
console.error(path, e);
return;
}
try {
var d = JSON.parse(content.toString("utf-8"));
} catch(e) {
console.error(path, e);
return;
}
printObject([path], d);
});
}
function printObject(prefix, d) {
try {
if(typeof d != "string" && typeof d != "number") {
for(var k in d)
printObject(prefix.concat(k), d[k]);
return;
}
} catch(e) {}
console.log(prefix.join(" "), d);
}