-
Notifications
You must be signed in to change notification settings - Fork 0
/
javascript1.js
166 lines (129 loc) · 4.64 KB
/
javascript1.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
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
function parseJsonPath(json, path) {
// Split the path by '.' and '[*]'
const parts = path.split(/\.|\[\*\]/);
console.log( parts);
console.log("parts:" + parts);
let newParts = parts.reduce((acc, val) => acc.concat(val), []);
console.log("newParts:" + newParts);
// Split the path into individual components
const components = path.split('.');
// Initialize the result with the original JSON object
let result = json;
// Traverse through each component of the path
for (let component of components) {
// Check if the component is a wildcard
if (component === '[*]' || component.includes('[*]')) {
console.log("2" + component);
const [key, index] = component.split('[');
result = result[key];
console.log("key:" + key);
console.log("index:" + index);
console.log("key result" + JSON.stringify(result));
const sum = result.reduce((acc, val) => {
console.log("val:" + JSON.stringify(val));
let val1 = val["title"];
return acc.concat(val1);
}, []);
console.log("sum:" + sum);
result = result.reduce((acc, val) => acc.concat(val), []);
console.log("27" + result);
console.log(JSON.stringify(result));
} else if (component.includes('[') && component.includes(']')) {
// Check if the component is an array index
console.log("3" + component);
const [key, index] = component.split('[');
const arrayIndex = parseInt(index.slice(0, -1));
result = result[key][arrayIndex];
console.log("3" + result);
} else {
// Otherwise, proceed as usual
result = result[component];
}
// If the result is undefined, return null (indicating data not found)
if (result === undefined) {
return null;
}
}
// Return the final result
return result;
}
// Example JSON object
const jsonData = {
"store": {
"book": [
{ "title": "Book 1", "author": "Author 1" },
{ "title": "Book 2", "author": "Author 2" },
{ "title": "Book 3", "author": "Author 3" }
]
}
};
// Example JSONPath expression with wildcard
const jsonPath = 'store.book[*].title';
// Parse JSONPath and find data in JSON object
const result = parseJsonPath(jsonData, jsonPath);
console.log(result); // Output: ["Book 1", "Book 2", "Book 3"]
*************Not Working*****************
function parseJsonPath(json, path) {
// Split the path by '.' and '[*]'
const parts = path.split(/\.|\[\*\]/);
console.log( parts);
console.log("parts:" + parts);
let newParts = parts.reduce((acc, val) => acc.concat(val), []);
console.log("newParts:" + newParts);
// Split the path into individual components
const components = path.split('.');
// Initialize the result with the original JSON object
let result = json;
// Traverse through each component of the path
for (let component of components) {
// Check if the component is a wildcard
if (component === '[*]' || component.includes('[*]')) {
console.log("2" + component);
const [key, index] = component.split('[');
result = result[key];
console.log("key:" + key);
console.log("index:" + index);
console.log("key result" + JSON.stringify(result));
const sum = result.reduce((acc, val) => {
console.log("val:" + JSON.stringify(val));
let val1 = val["title"];
return acc.concat(val1);
}, []);
console.log("sum:" + sum);
result = result.reduce((acc, val) => acc.concat(val), []);
console.log("27" + result);
console.log(JSON.stringify(result));
} else if (component.includes('[') && component.includes(']')) {
// Check if the component is an array index
console.log("3" + component);
const [key, index] = component.split('[');
const arrayIndex = parseInt(index.slice(0, -1));
result = result[key][arrayIndex];
console.log("3" + result);
} else {
// Otherwise, proceed as usual
result = result[component];
}
// If the result is undefined, return null (indicating data not found)
if (result === undefined) {
return null;
}
}
// Return the final result
return result;
}
// Example JSON object
const jsonData = {
"store": {
"book": [
{ "title": "Book 1", "author": "Author 1" },
{ "title": "Book 2", "author": "Author 2" },
{ "title": "Book 3", "author": "Author 3" }
]
}
};
// Example JSONPath expression with wildcard
const jsonPath = 'store.book[*].title';
// Parse JSONPath and find data in JSON object
const result = parseJsonPath(jsonData, jsonPath);
console.log(result); // Output: ["Book 1", "Book 2", "Book 3"]