Skip to content

Instantly share code, notes, and snippets.

@tidwall
Last active September 16, 2017 21:55
Show Gist options
  • Save tidwall/5f99540d8a718a23417b105b4c0f594e to your computer and use it in GitHub Desktop.
Save tidwall/5f99540d8a718a23417b105b4c0f594e to your computer and use it in GitHub Desktop.
get parent from child array path
package main
import (
"fmt"
"github.com/tidwall/gjson"
)
func main() {
json := `
{
"sites": [
{
"serverNames":[
"domain1.org"
],
"documentRoot": "./sites/domain1",
},
{
"serverNames":[
"domain2.org"
],
"documentRoot": "./sites/domain2",
}
]
}
`
site := getSite(json, "domain1.org")
fmt.Println(site.Raw)
}
func getSite(json, serverName string) gjson.Result {
var done bool
var site gjson.Result
gjson.Get(json, "sites").ForEach(func(_, rsite gjson.Result) bool {
rsite.Get("serverNames").ForEach(func(_, vserverName gjson.Result) bool {
if vserverName.String() == serverName {
site = rsite
done = true
}
return !done
})
return !done
})
return site
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment