https://github.com/romanyx/jwalk
Walk through JSON with Go
https://github.com/romanyx/jwalk
golang json
Last synced: 5 months ago
JSON representation
Walk through JSON with Go
- Host: GitHub
- URL: https://github.com/romanyx/jwalk
- Owner: romanyx
- License: mit
- Created: 2018-06-29T21:41:13.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2022-07-15T10:16:24.000Z (almost 4 years ago)
- Last Synced: 2024-11-17T12:18:15.955Z (over 1 year ago)
- Topics: golang, json
- Language: Go
- Size: 8.79 KB
- Stars: 66
- Watchers: 2
- Forks: 3
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
[](https://godoc.org/github.com/romanyx/jwalk)
[](https://goreportcard.com/report/github.com/romanyx/jwalk)
[](https://travis-ci.org/romanyx/jwalk)
# jwalk
Jwalk is built on top of easyjson/jlexer, and allows to easily unmarshal any JSON input with arbitrary key names by walking through it.
## Usage
```go
package main
import (
"fmt"
"log"
"github.com/romanyx/jwalk"
)
const input = `{
"key1": [{
"id": 1
}, {
"id": 2
}],
"key2": [1,2],
"key3": {
"id": 1,
"name": null
}
}`
func main() {
i, err := jwalk.Parse([]byte(input))
if err != nil {
log.Fatal(err)
}
switch v := i.(type) {
case jwalk.ObjectWalker:
v.Walk(func(key string, value interface{}) error {
fmt.Println(key + ":")
switch v := value.(type) {
case jwalk.ObjectsWalker:
v.Walk(func(obj jwalk.ObjectWalker) error {
fmt.Println("\t-")
obj.Walk(func(key string, value interface{}) error {
if v, ok := value.(jwalk.Value); ok {
fmt.Println("\t", key+":", v.Interface())
}
return nil
})
return nil
})
case jwalk.Value:
fmt.Println("\t", v.Interface())
case jwalk.ObjectWalker:
v.Walk(func(key string, value interface{}) error {
if v, ok := value.(jwalk.Value); ok {
fmt.Println("\t", key+":", v.Interface())
}
return nil
})
}
return nil
})
}
}
```
## Testing
```bash
go test
```
## Contributing
Please feel free to submit issues, fork the repository and send pull requests!