https://github.com/iamfaizankhalid/jsonq
Easily traverse through the JSON data with no fixed structure.
https://github.com/iamfaizankhalid/jsonq
go json jsonquery traversal
Last synced: 11 months ago
JSON representation
Easily traverse through the JSON data with no fixed structure.
- Host: GitHub
- URL: https://github.com/iamfaizankhalid/jsonq
- Owner: IamFaizanKhalid
- License: mit
- Created: 2023-08-04T11:58:38.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-08-13T07:50:06.000Z (over 2 years ago)
- Last Synced: 2025-01-18T01:10:35.625Z (about 1 year ago)
- Topics: go, json, jsonquery, traversal
- Language: Go
- Homepage:
- Size: 8.79 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# jsonq

[](https://goreportcard.com/report/github.com/IamFaizanKhalid/jsonq) [](https://github.com/IamFaizanKhalid/jsonq/releases) [](./LICENSE) [](https://pkg.go.dev/github.com/IamFaizanKhalid/jsonq)
Easily traverse through the JSON data with no fixed structure.
## Installation
```shell
go get -u github.com/IamFaizanKhalid/jsonq
```
## Usage Example
See [reference docs](https://pkg.go.dev/github.com/IamFaizanKhalid/jsonq) for more details.
```go
package main
import (
"fmt"
"github.com/IamFaizanKhalid/jsonq"
)
func main() {
apiResponse := []byte(`{
"code": 200,
"status": "success",
"data": {
"user": {
"id": 123,
"name": "John Doe",
"email": "john@example.com"
},
"posts": [
{
"id": 1,
"title": "First Post"
},
{
"id": 2,
"title": "Second Post"
}
]
}
}`)
// parsing the JSON object
resp, err := jsonq.ParseObject(apiResponse)
if err != nil {
panic(err)
}
// getting values from the object
code := resp.Val("code").Int()
status := resp.Val("status").Str()
fmt.Println(code, status)
// checking if the object contains the sub-object
if !resp.Has("data") {
return
}
data := resp.Obj("data")
// getting a sub-object which is optional
userInfo, ok := data.OptObj("user")
if ok {
fmt.Println("-- User Info --")
fmt.Println("ID:", userInfo.Val("id").Int())
fmt.Println("Name:", userInfo.Val("name").Str())
fmt.Println("Email:", userInfo.Val("email").Str())
}
fmt.Println("-- Posts --")
// getting array of objects
posts := data.Arr("posts").Obj()
for _, post := range posts {
id := post.Val("id").Int()
title := post.Val("title").Str()
fmt.Println(id, title)
}
}
```
## License
[MIT License](./LICENSE)