a jq like library for golang that helps parse nested json
go get github.com/BatteredBunny/rjson
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"
}
{
"uwu": {
"nya": "123"
},
"one": {
"two": {
"three": {
"num": 1
}
},
"arr": [
"a",
"b"
]
}
}
For a more complete example have a look at tag_test.go
If the json isnt parsing as expected try enabling the rjson.Debug variable.
rjson.Debug = true
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
- Dot symbol is used as path seperator, e.g
one.two.three
- You can index slices/array like you would normally, e.g
arr[0]
,arr[1]
- You can access the last value of an slices/array using this, e.g
arr[-]
-
e.g
arr[].text
{ "arr": [ { "text": "1", "num": 1 }, { "text": "2", "num": 2 }, { "no_commons": "this object has no common fields, so it wont be included" } ] }
[ { "text": "1", }, { "text": "2", } ]