Handle JSON with ease in golang.
About
Goson was created to simplify reading JSON data within Golang. This library has been inspired by SwiftyJSON
Install
go get github.com/panthesingh/goson
Starting
Create a goson object from JSON data. Returns an error if the data is not valid JSON.
g, err := goson.Parse(data)
Data
Get()float64intboolstring
name := g.Get("name").String() age := g.Get("age").Int() weight := g.Get("weight").Float() married := g.Get("married").Bool()
Chaining
Chaining is a nice way to quickly traverse the data and grab what you need.
g.Get("key").Get("object").Index(0).Get("item").String()
Existance
Value()interface{}
v, ok := g.Get("key").Value().(string) if !ok { println("key does not exist") }
Loop
Len()len()Index()
for i := 0; i < g.Len(); i++ { name := g.Index(i).Get("name").String() age := g.Index(i).Get("age").Int() }
Printing
String()
v := g.Get("child") fmt.Println(v)
Example
package main import ( "github.com/panthesingh/goson" ) func main() { json := `{ "name": "Bob", "age": 100, "cars": [ "Honda", "Toyota" ], "details": { "weight": 160.5, "married": false } }` g, _ := goson.Parse([]byte(json)) name := g.Get("name").String() age := g.Get("age").Int() cars := g.Get("cars") carOne := car.Index(0).String() carTwo := car.Index(1).String() weight := g.Get("details").Get("weight").String() married := g.Get("details").Get("married").Bool() }
Documentation
Documentation can be found on godoc: