-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjson_data_dictionary.js
More file actions
executable file
·174 lines (163 loc) · 4.77 KB
/
json_data_dictionary.js
File metadata and controls
executable file
·174 lines (163 loc) · 4.77 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
#!/usr/bin/env node
import { readFileSync, existsSync } from 'node:fs';
import { hideBin } from 'yargs/helpers';
import yargs from 'yargs';
const argv = yargs(hideBin(process.argv))
.parserConfiguration({"boolean-negation": true})
.option('v', {
alias: 'verbose',
describe: 'Logs all values, not just types',
default: false,
type: 'boolean'})
.option('s', {
alias: 'sorted',
describe: 'Sorts the output',
default: true,
type: 'boolean'})
.option('k', {
alias: 'keys',
describe: 'Outputs a list of unique keys found',
default: false,
type: 'boolean'})
.option('f', {
alias: 'flatten',
describe: 'Flatten array indexes to single value (by default "[]")',
default: true,
type: 'any'})
.option('g', {
alias: 'ignore',
describe: 'Ignore and skip all keys matching this regex pattern',
default: null,
type: 'string'})
.option('l', {
alias: 'jsonl',
describe: 'Expect JSONL in input files',
default: false,
type: 'boolean'})
.parse();
const isObject = (v) => v && (typeof v === 'object') && !('length' in v);
const isArray = (v) => v && (typeof v === 'object') && ('length' in v);
const getJSONKeyPaths = (v, result=[], keyPath=[], keySet, verbose, indexRepl, ignore, depth) => {
// let result = !_result ? [] : _result;
// let keyPath = !_keyPath ? [] : _keyPath;
// if (depth > 5) {
// console.log("Depth", depth);
// }
if(isArray(v)) {
for(let i=0; i<v.length; i++) {
getJSONKeyPaths(
v[i],
result,
keyPath.concat(indexRepl ? indexRepl : i),
keySet,
verbose,
indexRepl,
ignore,
depth + 1,
);
}
} else if(isObject(v)) {
for(let k in v) {
if (ignore && ignore.test(k))
continue;
keySet.add(k);
getJSONKeyPaths(
v[k],
result,
keyPath.concat(k),
keySet,
verbose,
indexRepl,
ignore,
depth + 1,
);
}
} else {
let endPoint = keyPath.join('.') + ' = ' + (
verbose ? JSON.stringify(v) : typeof(v)
);
result.add(endPoint);
}
}
class UniqueCollection extends Set {
push(v) {
if(!this.has(v)) {
super.add(v);
}
}
sort() {
return [...this].sort();
}
}
// Run from CLI (i.e. not invoked via gulp)
if (import.meta.url === `file://${process.argv[1]}`) {
if(argv._.length < 1) {
console.log(
'Requires at least one JSON file as argument. ' +
'Use --help for further information.');
process.exit(1);
}
// Process each file
let res = argv.sorted ? new UniqueCollection() : [];
let keySet = new Set();
let keyPath = [];
const ignore = RegExp(argv.ignore);
// console.log("ignore", ignore);
// console.log('argv', argv);
// console.log('argv._', argv._);
for (let pathname of argv._) {
if (!existsSync(pathname))
continue;
// console.log("pathname", pathname);
let data = readFileSync(pathname, 'utf-8');
if (argv.jsonl) {
if (data.indexOf("\n") >= 0) {
data = data.split("\n");
}
} else {
data = [data]
}
// console.log(1);
let line = 0;
for (const [index, thisData] of data.entries()) {
// console.log(index);
// if(line % 2500) {
// console.log(line++);
// }
if (thisData) {
getJSONKeyPaths(
JSON.parse(thisData),
res,
keyPath,
keySet,
argv.verbose,
argv.flatten ?
typeof argv.flatten == 'string' ? argv.flatten : '[]'
: false,
ignore,
0,
);
}
}
}
// Output results
if(argv.sorted) {
for (let r of res.sort()) {
console.log(r);
}
} else {
console.log(res.join('\n'));
}
// Output unique keys
if(argv.keys) {
if (argv.sorted) {
keySet = [...keySet].sort();
}
for(let k of keySet) {
console.log(k);
}
}
}
// Character strings to binary
// Array.from(new TextEncoder().encode('\x12\x02\b\x04'), bits=>bits.toString(2).padStart(8,'0'),).join('');
// t="";for(i=0;d='\x12\x02\b\x04'.charCodeAt(i/8);)t+=d>>(7-i++&7)&1