https://github.com/batteredbunny/rjson
a jq like library for golang that helps parse nested json
https://github.com/batteredbunny/rjson
golang json
Last synced: about 2 months ago
JSON representation
a jq like library for golang that helps parse nested json
- Host: GitHub
- URL: https://github.com/batteredbunny/rjson
- Owner: BatteredBunny
- License: gpl-3.0
- Created: 2023-07-12T00:14:26.000Z (almost 3 years ago)
- Default Branch: main
- Last Pushed: 2024-01-07T00:54:09.000Z (over 2 years ago)
- Last Synced: 2025-02-05T23:02:36.595Z (over 1 year ago)
- Topics: golang, json
- Language: Go
- Homepage:
- Size: 84 KB
- Stars: 3
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
rjson
a jq like library for golang that helps parse nested json
```
go get github.com/BatteredBunny/rjson
```
## Parse nested json without any anonymous structs
```go
type Out struct {
One string `rjson:"uwu.nya"` // "123"
Two int `rjson:"one.two.three.num"` // 1
Three []string `rjson:"one.arr"` // ["a","b"]
Four string `rjson:"one.arr[0]"` // "a"
}
```
```json
{
"uwu": {
"nya": "123"
},
"one": {
"two": {
"three": {
"num": 1
}
},
"arr": [
"a",
"b"
]
}
}
```
For a more complete example have a look at `tag_test.go`
## Try out the parsing in an interactive form

```
go run github.com/BatteredBunny/rjson/cmd/livejson --file example.json
```
## Helpful
### Debugging
If the json isnt parsing as expected try enabling the rjson.Debug variable.
```rjson.Debug = true```
### Jetbrains
For quickly parsing json, in jetbrains IDE you can directly copy the json pointer and paste it into rjson field tag

copied value: ``onResponseReceivedActions[0].appendContinuationItemsAction.continuationItems[76].playlistVideoRenderer.thumbnail.thumbnails``
## Syntax explanation
### Path seperator: "."
- Dot symbol is used as path seperator, e.g `one.two.three`
### Array index: [0]
- You can index slices/array like you would normally, e.g `arr[0]`, `arr[1]`
### Last value: [-]
- You can access the last value of an slices/array using this, e.g `arr[-]`
### Value iterator: []
- e.g `arr[].text`
#### Input
```json
{
"arr": [
{
"text": "1",
"num": 1
},
{
"text": "2",
"num": 2
},
{
"no_commons": "this object has no common fields, so it wont be included"
}
]
}
```
#### Output
```json
[
{
"text": "1",
},
{
"text": "2",
}
]
```