loutian / goson

Handle JSON with ease in golang.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Goson

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

Every Get() call will return another goson object. You can access the underlying data with a value function. The default value types are float64, int, bool, string. If the key does not exist the function will return the default zero value. To check if a key exists read the section on existence.

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

To check if a value exists use a type check on the Value() function. This returns the underlying value as an interface{}.

v, ok := g.Get("key").Value().(string)
if !ok {
  println("key does not exist")
}

Loop

Calling Len() will return len() on the underlying value. You can use the Index() function to loop through all the values.

for i := 0; i < g.Len(); i++ {
    name := g.Index(i).Get("name").String()
    age := g.Index(i).Get("age").Int()
}

Printing

A very useful feature is pretty printing the JSON structure at any value. Likewise calling String() returns the same 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:

https://godoc.org/github.com/panthesingh/goson

Author

Panthe Singh, http://twitter.com/panthesingh

About

Handle JSON with ease in golang.

License:MIT License


Languages

Language:Go 100.0%