Jason intends to be an idiomatic JSON library for Go. Inspired by other libraries and improved to work well for common use cases. It currently focuses on reading JSON data rather than creating it. API Documentation can be found on godoc.org.
Note: The API will be subject to change during 2014 if there are very good reasons to do so. On January 1st 2015 it will be frozen.
The following golang values are used for the JSON data types. It is consistent with how encoding/json
uses primitive types.
bool
, for JSON booleansfloat64
, for JSON numbersstring
, for JSON strings[]*Jason
, for JSON arraysmap[string]*Jason
, for JSON objectsnil
for JSON null
go get github.com/antonholmquist/jason`
import (
"github.com/antonholmquist/jason"
)
Create a instance from a string. Returns an error if the string couldn't be parsed.
root, err := jason.NewFromString(s)
Create a instance from a net/http response. Returns an error if the string couldn't be parsed.
root, err := jason.NewFromReader(res.Body)
Reading values is easy. If the key is invalid, it will return the default value.
root.Get("name").String()
root.Get("age").Number()
root.Get("verified").Boolean()
root.Get("education").Object()
root.Get("friends").Array()
Reading nested values is easy. If the path is invalid, it will return the default value, for instance the empty string.
root.Get("person", "name").String()
root.Get("person", "age").Number()
root.Get("person", "verified").Boolean()
root.Get("person", "education").Object()
root.Get("person", "friends").Array()
To check if a value exist, use Has()
or Exists()
. The two examples below are identical and have different use cases.
root.Has("person", "name")
root.Get("person", "name").Exists()
To check if a value at the keypath really is what you think it is, use the Is()-methods
.
root.Get("name").IsString()
root.Get("age").IsNumber()
root.Get("verified").IsBoolean()
root.Get("education").IsObject()
root.Get("friends").IsArray()
root.Get("friends").IsNull()
Looping through an array is easy and will never return an exeption. Array()
returns an empty slice if the value at that keypath is null (or something else than an array).
for _, friend := range person.Get("friends").Array() {
name := friend.Get("name").String()
age := friend.Get("age").Number()
}
Looping through an object is easy and will never return an exeption. Object()
returns an empty map if the value at that keypath is null (or something else than an object).
for key, value := person.Get("person").Object() {
...
}
Example project demonstrating how to parse a string.
package main
import (
"github.com/antonholmquist/jason"
"log"
)
func main() {
exampleJSON := `{
"name": "Walter White",
"age": 51,
"children": [
"junior",
"holly"
],
"other": {
"occupation": "chemist",
"years": 23
}
}`
j, _ := jason.NewFromString(exampleJSON)
log.Println("name:", j.Get("name").String())
log.Println("age:", j.Get("age").Number())
log.Println("occupation:", j.Get("other", "occupation").String())
log.Println("years:", j.Get("other", "years").Number())
for i, child := range j.Get("children").Array() {
log.Printf("child %d: %s", i, child.String())
}
}
Documentation can be found a godoc:
https://godoc.org/github.com/antonholmquist/jason
To run the project tests:
go test