Get JSON values quickly
- go (>= 1.11)
Create gson object from []byte
. Returns an error if the bytes are not valid json.
g, err := gson.CreateWithBytes(data)
Create gson object from a io.Reader
. Returns an error if the resp.Body are not valid json.
g, err := gson.CreateWithReader(resp.Body)
GetByPath
gets json value for specified path. The path is syntax such as created_at.date
. And if you want to get the elements of json array, please put number in keys such as likes.0
.
var json = `
{
"id": "1111",
"name": "hlts2",
"likes": [
"apple",
"strawberry",
"pineapple"
],
"created_at": {
"date": "2017-05-10 12:54:18",
"timezone": "UTC"
}
}
`
g, _ := gson.CreateWithBytes([]byte(json))
result, _ := g.GetByPath("likes.1")
str := result.String() // str, err := result.StringE()
fmt.Println(str) //strawberry
{
"name":{"first":"little", "last":"tiny"},
"age":24,
"children":["hiroto", "haruki"],
"friends":[
{
"id":0,
"name":"Beck Hansen"
},
{
"id":1,
"name":"Cleveland Gomez"
},
{
"id":2,
"name":"Norton Duncan"
}
]
}
"name.last" >> "tiny"
"age" >> 24
"children" >> ["hiroto", "haruki"]
"children.1" >> "haruki"
"friends.1" >> {"id":1,"name":"Cleveland Gomez"}
"friends.1.id" >> 1
"friends.#.id" >> [0, 1, 2]
GetByKeys
gets json value for specified keys. keys are given as string slice such as []string{"created_at", "date"}
. And if you want to get the elements of json array, please put number in keys such as []string{"likes", "0"}
.
var json = `
{
"id": "1111",
"name": "hlts2",
"likes": [
"apple",
"strawberry",
"pineapple"
],
"created_at": {
"date": "2017-05-10 12:54:18",
"timezone": "UTC"
}
}
`
g, _ := gson.CreateWithBytes([]byte(json))
result, _ := g.GetByKeys("likes", "1")
str := result.String() // str, err := result.StringE()
fmt.Println(str) //strawberry
json := `{"created_at": {"date": "2017-05-10 12:54:18"}}`
g, _ := gson.CreateWithBytes([]byte(json))
result, _ := g.GetByKeys("created_at")
m := result.Map() // m, err := result.MapE()
for key, value := range m {
fmt.Printf("key: %s, value: %v", key, value.Interface{}) //key: date, value: 2017-05-10 12:54:18
}
json := `{"Likes": ["pen"]}`
g, _ := gson.CreateWithBytes([]byte(json))
result, _ := g.GetByKeys("Likes")
s := result.Slice() // s, err := result.SliceE()
for _, value := range s {
fmt.Printf("value: %v", value.Interface()) //value: pen
}
Indent
returns the formatted json string
json := `{"Accounts": [{"ID": "1111"}, {"ID": "2222"}]}`
g, _ := gson.CreateWithBytes([]byte(json))
var buf bytes.Buffer
g.Indent(&buf, "", " ")
fmt.Println(buf.String())
/*
{
"IDs": [
{
"ID": "1111"
},
{
"ID": "2222"
}
]
}
*/