-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
74 lines (63 loc) · 1.57 KB
/
main.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
package main
import (
"fmt"
"github.com/Jeffail/gabs"
)
func main() {
data := []byte(`{
"employees":{
"protected":false,
"address":{
"street":"22 Saint-Lazare",
"postalCode":"75003",
"city":"Paris",
"countryCode":"FRA",
"country":"France"
},
"employee":[
{
"id":1,
"first_name":"Jeanette",
"last_name":"Penddreth"
},
{
"id":2,
"firstName":"Giavani",
"lastName":"Frediani"
}
]
}
}`)
jsonParsed, err := gabs.ParseJSON(data)
if err != nil {
panic(err)
}
fmt.Println("Get value of Protected:\t", jsonParsed.Path("employees.protected").Data())
fmt.Println("Get value of Country:\t", jsonParsed.Search("employees", "address", "country").Data())
fmt.Println("ID of first employee:\t", jsonParsed.Path("employees.employee.0.id").String())
fmt.Println("Check Country Exists:\t", jsonParsed.Exists("employees", "address", "countryCode"))
addressJson, err := jsonParsed.Search("employees", "address").ChildrenMap()
if err != nil {
fmt.Println("Error:", err)
return
}
for key, child := range addressJson {
fmt.Printf("Key=>%v, Value=>%v\n", key, child.Data().(string))
}
employee, err := jsonParsed.Search("employees", "employee").Children()
if err != nil {
fmt.Println("Error:", err)
return
}
for _, child := range employee {
fmt.Println(child.Data())
}
firstEmployee, err := jsonParsed.Search("employees", "employee", "0").Children()
if err != nil {
fmt.Println("Error:", err)
return
}
for _, child := range firstEmployee {
fmt.Println(child.Data())
}
}