-
Notifications
You must be signed in to change notification settings - Fork 0
/
json.go
272 lines (227 loc) · 6.81 KB
/
json.go
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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
package finder
import (
"github.com/antchfx/jsonquery"
"os"
"strings"
)
func GetJSONDocumentFromFile(filename string) *jsonquery.Node {
// Get JSON file
//f, err := os.Open(filename)
// Parse JSON file
data, err := os.ReadFile(filename)
if err != nil {
log.Fatalf("Reading JSON input file failed: %s %s", err.Error(), filename)
}
jsonlString := ConvertJSONLtoJSON(string(data))
jsonReader := strings.NewReader(jsonlString)
input, err := jsonquery.Parse(jsonReader)
if err != nil {
log.Fatalf("Reading JSON input file failed: %s %s", err.Error(), filename)
}
return input
}
/*
-----------------------------------------
Interesting info extraction section
-----------------------------------------
*/
func GetValueForQueryKey(document *jsonquery.Node, requestedField string, queryKey string, query []string) []string {
return getValuesFromEntriesViaContains(document, requestedField, queryKey, query)
}
func GetValueForQueryBoolean(document *jsonquery.Node, requestedField string, queryKey string, matchCondition bool) []string {
return getValuesFromEntriesMatchBoolean(document, requestedField, queryKey, matchCondition)
}
func GetAllValuesForKey(document *jsonquery.Node, key string) []string {
return getValuesFromAllEntries(document, key)
}
func GetAllRecordsForKey(document *jsonquery.Node, key string) []*jsonquery.Node {
return getAllNodesForKey(document, key)
}
/*
HELPER FUNCTIONS
*/
// query = test
// {"test1":"..", "test2":..,"asdfasdftest":..,*}
func getValuesFromEntriesViaContains(document *jsonquery.Node, key string, queryKey string, queryContains []string) []string {
var result []string
for _, query := range queryContains {
entries, error := jsonquery.QueryAll(document, "//*/"+queryKey+"[contains(.,"+query+")]")
if error != nil {
log.Errorf("Querying JSON error #%v ", error)
}
for _, entry := range entries {
if entryValues, ok := entry.Parent.Value().(map[string]interface{}); ok {
values, exists := entryValues[key]
if exists {
if value, ok := values.(string); ok {
if key == "url" {
result = AppendIfMissing(result, GetHost(value))
} else {
result = AppendIfMissing(result, value)
}
}
}
}
}
}
return result
}
// {"queryKey":"query" => "key":...}
func getNodesFromSpecificQueryViaEquals(document *jsonquery.Node, queryKey string, query string) []*jsonquery.Node {
//var result []string
entries, error := jsonquery.QueryAll(document, "*["+queryKey+"='"+query+"']")
if error != nil {
log.Errorf("Querying JSON error #%v ", error)
}
return entries
}
// *[isbn='0-553-21311-3']
func getValuesFromEntriesMatchBoolean(document *jsonquery.Node, key string, queryKey string, matchCondition bool) []string {
var result []string
var query string
if matchCondition {
query = "//*[" + queryKey + "='true']"
} else {
query = "//*[" + queryKey + "='false']"
}
entries, error := jsonquery.QueryAll(document, query)
if error != nil {
log.Errorf("Querying JSON error #%v ", error)
}
for _, entry := range entries {
if entryValues, ok := entry.Parent.Value().(map[string]interface{}); ok {
values, exists := entryValues[key]
if exists {
if value, ok := values.(string); ok {
if key == "url" {
result = AppendIfMissing(result, GetHost(value))
} else {
result = AppendIfMissing(result, value)
}
}
}
}
}
return result
}
func getAllNodesByContains(document *jsonquery.Node, queryKey string, queryContains []string) []*jsonquery.Node {
var results []*jsonquery.Node
for _, query := range queryContains {
entries, error := jsonquery.QueryAll(document, "//*/"+queryKey+"[contains(.,'"+query+"')]")
if error != nil {
log.Errorf("Querying JSON error #%v ", error)
}
for _, entry := range entries {
results = append(results, entry)
}
}
return results
}
func getValuesForKey(document *jsonquery.Node, key string) []string {
var result []string
entries, error := jsonquery.QueryAll(document, "//*")
if error != nil {
log.Errorf("Querying JSON error #%v ", error)
}
for _, entry := range entries {
if entryValues, ok := entry.Value().(map[string]interface{}); ok {
values, exists := entryValues[key]
if exists {
if entries, ok := values.([]interface{}); ok {
for _, subValues := range entries {
if subValue, ok := subValues.(string); ok {
result = AppendIfMissing(result, subValue)
}
}
} else {
if value, ok := values.(string); ok {
result = AppendIfMissing(result, value)
}
}
}
}
}
return result
}
func getAllNodesForKey(document *jsonquery.Node, key string) []*jsonquery.Node {
entries, error := jsonquery.QueryAll(document, "//*["+key+"]")
if error != nil {
log.Errorf("Querying JSON error #%v ", error)
}
return entries
}
func getValuesFromAllEntries(document *jsonquery.Node, key string) []string {
var result []string
entries, error := jsonquery.QueryAll(document, "//"+key)
if error != nil {
log.Errorf("Querying JSON error #%v ", error)
}
for _, entry := range entries {
if entryValues, ok := entry.Parent.Value().(map[string]interface{}); ok {
values, exists := entryValues[key]
if exists {
if value, ok := values.(string); ok {
if key == "url" {
result = AppendIfMissing(result, GetHost(value))
} else {
if url, ok := entryValues["url"].(string); ok {
completeValue := value + " (" + url + ")"
result = AppendIfMissing(result, completeValue)
}
}
} else if value, ok := values.([]interface{}); ok {
if key == "subject_an" {
for _, val := range value {
if entry, ok := val.(string); ok {
result = AppendIfMissing(result, entry)
}
}
}
}
}
}
}
return result
}
func getValuesFromAllNodes(nodes []*jsonquery.Node, key string) []string {
var result []string
for _, recordEntry := range nodes {
if entryValues, ok := recordEntry.Value().(map[string]interface{}); ok {
values, exists := entryValues[key]
if exists {
if entries, ok := values.([]interface{}); ok {
for _, subValues := range entries {
if subValue, ok := subValues.(string); ok {
result = AppendIfMissing(result, subValue)
}
}
} else {
if entry, ok := values.(string); ok {
result = AppendIfMissing(result, entry)
}
}
}
}
}
return result
}
func getValuesFromNode(node *jsonquery.Node, key string) []string {
var result []string
if entryValues, ok := node.Value().(map[string]interface{}); ok {
values, exists := entryValues[key]
if exists {
if entries, ok := values.([]interface{}); ok {
for _, subValues := range entries {
if subValue, ok := subValues.(string); ok {
result = AppendIfMissing(result, subValue)
}
}
} else {
if entry, ok := values.(string); ok {
result = AppendIfMissing(result, entry)
}
}
}
}
return result
}