Last active
September 16, 2017 21:55
-
-
Save tidwall/5f99540d8a718a23417b105b4c0f594e to your computer and use it in GitHub Desktop.
get parent from child array path
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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