-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathjson_parser.js
41 lines (33 loc) · 891 Bytes
/
json_parser.js
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
import fs from "fs"
import jsonic from "jsonic"
function getJSON(file) {
let output = fs.readFileSync(file, "utf8").trim();
if (output.endsWith(";"))
output = output.slice(0, -1);
output = output.replace(/(?<!")\btrue\w+/g, `"$&"`);
output = output.replace(/\s+/g,' ');
return output;
}
function validate(buffer) {
try {
jsonic(buffer)
} catch (error) {
console.error(error.message)
}
}
export function propFilter(file, props, entityType) {
const json = getJSON(file);
let parsed;
validate(json);
parsed = jsonic(json);
Object.keys(parsed[entityType]).forEach(key => getProps(parsed[entityType], key));
return parsed;
function getProps(elem, key) {
const entry = {};
Object.keys(elem[key]).forEach(prop => props.forEach(pkey => {
if (prop === pkey)
entry[prop] = elem[key][prop];
}));
elem[key] = entry;
}
}