https://github.com/xiaost/jsonport
a simple and high performance json reader without pain
https://github.com/xiaost/jsonport
golang json
Last synced: about 1 year ago
JSON representation
a simple and high performance json reader without pain
- Host: GitHub
- URL: https://github.com/xiaost/jsonport
- Owner: xiaost
- License: mit
- Created: 2015-07-30T17:57:59.000Z (almost 11 years ago)
- Default Branch: master
- Last Pushed: 2020-04-02T15:32:07.000Z (about 6 years ago)
- Last Synced: 2025-03-25T07:51:07.561Z (over 1 year ago)
- Topics: golang, json
- Language: Go
- Homepage:
- Size: 23.4 KB
- Stars: 16
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
jsonport [](https://godoc.org/github.com/xiaost/jsonport)
====
jsonport is a simple and high performance golang package for accessing json without pain. features:
* No reflection.
* Unmarshal without struct.
* Unmarshal for the given json path only.
* 2x faster than encoding/json.
It is inspired by [jmoiron/jsonq](https://github.com/jmoiron/jsonq). Feel free to post issues or PRs, I will reply ASAP :-)
## Usage
```go
package main
import (
"fmt"
"github.com/xiaost/jsonport"
)
func main() {
jsonstr := `{
"timestamp": "1438194274",
"users": [{"id": 1, "name": "Tom"}, {"id": 2, "name": "Peter"}],
"keywords": ["golang", "json"],
"status": 1
}`
j, _ := jsonport.Unmarshal([]byte(jsonstr))
fmt.Println(j.GetString("users", 0, "name")) // Tom, nil
fmt.Println(j.Get("keywords").StringArray()) // [golang json], nil
fmt.Println(j.Get("users").EachOf("name").StringArray()) // [Tom Peter], nil
// try parse STRING as NUMBER
fmt.Println(j.Get("timestamp").Int()) // 0, type mismatch: expected NUMBER, found STRING
j.StringAsNumber()
fmt.Println(j.Get("timestamp").Int()) // 1438194274, nil
// convert NUMBER, STRING, ARRAY and OBJECT type to BOOL
fmt.Println(j.GetBool("status")) // false, type mismatch: expected BOOL, found NUMBER
j.AllAsBool()
fmt.Println(j.GetBool("status")) // true, nil
// using Unmarshal with path which can speed up json decode
j, _ = jsonport.Unmarshal([]byte(jsonstr), "users", 1, "name")
fmt.Println(j.String()) // Peter, nil
}
```
For more information on getting started with `jsonport` [check out the doc](https://godoc.org/github.com/xiaost/jsonport)