-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathqueryJson.go
45 lines (40 loc) · 888 Bytes
/
queryJson.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
package go_utils
import (
"fmt"
"github.com/itchyny/gojq"
"github.com/simonnilsson/ask"
"reflect"
)
func IsPointer(x interface{}) bool {
return reflect.TypeOf(x).Kind() == reflect.Ptr
}
// for github.com/itchyny/gojq
func GetJQ(source interface{}, path string) interface{} {
//var lk = GetLock("GetJQ").Lock()
//defer lk.Unlock()
if m1, ok := source.(*map[string]interface{}); ok {
source = *m1
}
if ps, err := gojq.Parse(path); nil == err {
if data := ps.Run(source); nil != data {
if o, ok := data.Next(); ok {
return o
}
}
}
return nil
}
// itchyny/gojq
func GetJQ2Str(source interface{}, path string) string {
if ps := GetJQ(source, path); nil != ps {
return fmt.Sprintf("%v", ps)
}
return ""
}
func GetJson4Query(source interface{}, path string) interface{} {
res := ask.For(source, path)
if nil != res {
return res.Value()
}
return nil
}