forked from yazz/VisualJS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testjson.js
197 lines (157 loc) · 4.6 KB
/
testjson.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
var allPaths = new Object()
function pathToString(pp) {
var s = ""
for ( var aa = 0 ; aa < pp.length ; aa ++ ) {
s += pp[aa]
if (aa < pp.length -1) {
s += "."
}
}
return s
}
function addToPaths(path) {
var cpath = pathToString(path)
if (!allPaths[cpath]) {
allPaths[cpath] = {
count: 0,
path: path
}
}
allPaths[cpath].count ++
}
function isMap(o) {
try {
Map.prototype.has.call(o); // throws if o is not an object or has no [[MapData]]
return true;
} catch(e) {
if (typeof o === 'object') {
return true
}
return false;
}
}
function findJsonPaths(currentPath,jsonNode) {
addToPaths(currentPath)
if (Array.isArray(jsonNode)) {
//console.log("Found node: " )
for (var k = 0 ; k < jsonNode.length ; k++) {
//console.log("Key: " + k)
var newPath = currentPath.concat(["[]"])
findJsonPaths( newPath, jsonNode[k])
}
} else if (isMap(jsonNode)) {
var keys = Object.keys(jsonNode)
//console.log("Found map:.. " + keys.length)
for (var k = 0 ; k < keys.length ; k++) {
//console.log("Key: " + keys[k])
var newPath = currentPath.concat([keys[k]])
findJsonPaths( newPath, jsonNode[keys[k]])
}
} else if (typeof jsonNode === 'object') {
//console.log("Found object: " )
} else {
//console.log("Found other: " + JSON.stringify(jsonNode,null,2))
}
}
var a = {
"services": {
"service": [
{
"id": "2555417834325",
"account_id": "2445582847204",
"name": "API",
"state": "incomplete",
"system_name": "api",
"backend_version": "1",
"intentions_required": "false",
"buyers_manage_apps": "true",
"buyers_manage_keys": "true",
"referrer_filters_required": "false",
"custom_keys_enabled": "true",
"buyer_key_regenerate_enabled": "true",
"mandatory_app_key": "true",
"buyer_can_select_plan": "false",
"buyer_plan_change_permission": "request",
"deployment_option": "hosted",
"support_email": "[email protected]",
"end_user_registration_required": "true",
"metrics": {
"metric": [
{
"id": "2555418407956",
"name": "hits",
"system_name": "hits",
"friendly_name": "Hits",
"service_id": "2555417834325",
"description": "Number of API hits",
"unit": "hit"
},
{
"id": "2555418408699",
"name": "SASASA",
"system_name": "SASASA",
"friendly_name": "aaa",
"service_id": "2555417834325",
"unit": "XS"
}
],
"method": [
{
"id": "2555418408700",
"name": "hello",
"system_name": "hello",
"friendly_name": "jkjkjjkjkjk",
"service_id": "2555417834325",
"metric_id": "2555418407956"
},
{
"id": "2555418408703",
"name": "moo",
"system_name": "moo",
"friendly_name": "moo",
"service_id": "2555417834325",
"metric_id": "2555418407956"
}
]
}
},
{
"id": "2555417834330",
"account_id": "2445582847204",
"name": "dsasd",
"state": "incomplete",
"system_name": "dsds",
"backend_version": "1",
"intentions_required": "false",
"buyers_manage_apps": "true",
"buyers_manage_keys": "true",
"referrer_filters_required": "false",
"custom_keys_enabled": "true",
"buyer_key_regenerate_enabled": "true",
"mandatory_app_key": "true",
"buyer_can_select_plan": "false",
"buyer_plan_change_permission": "request",
"deployment_option": "hosted",
"support_email": "[email protected]",
"end_user_registration_required": "true",
"metrics": {
"metric": {
"id": "2555418407980",
"name": "hits",
"system_name": "hits",
"friendly_name": "Hits",
"service_id": "2555417834330",
"description": "Number of API hits",
"unit": "hit"
}
}
}
]
}
}
function main() {
findJsonPaths( [], a)
debugger
console.log(JSON.stringify(Object.keys(allPaths),null,2))
}
main()