-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
58 lines (48 loc) · 1.46 KB
/
index.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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
function stringParser(str) {
const result = [];
let current = '';
let isInQuotes = false;
for (let i = 0; i < str.length; i++) {
const char = str[i];
if (char === '"' && !isInQuotes) {
isInQuotes = true;
current += '"';
} else if (char === '"' && isInQuotes && str[i + 1] === '.') {
isInQuotes = false;
current += '"';
result.push(current);
current = '';
i++; // Skip the next character (.)
} else if (char === '.' && !isInQuotes) {
result.push(current);
current = '';
} else {
current += char;
}
}
result.push(current);
return result;
}
function jsonParser(json, path) {
const keys = stringParser(path);
let value = json;
for (let key of keys) {
if (typeof value !== 'object' || value === null) {
return undefined; // Invalid path or non-object value encountered
}
if (key.startsWith('"') && key.endsWith('"')) {
// Remove surrounding quotes from the key
key = key.slice(1, -1);
}
if (Array.isArray(value) && /^\d+$/.test(key)) {
// If the value is an array and the key is a non-negative integer, convert the key to a number
key = parseInt(key, 10);
}
value = value[key];
if (value === undefined) {
return undefined; // Key not found in the object
}
}
return value;
}
module.exports = jsonParser