Even though the question is old and solved in different ways, I came across an similiar problem and didn't find an easy solution. I only needed 1 of all the values in a huge json-response.
My approach: Simply using a regex over an given string, in this case the JSON formatted string.
The plain string gets filtered for a given key and returns only the value for the given key via this method
// extracts the value for a key from a JSON-formatted string
// body - the JSON-response as a string. Usually retrieved via the request body
// key - the key for which the value should be extracted
// returns - the value for the given key
func extractValue(body string, key string) string {
keystr := "\"" + key + "\":[^,;\\]}]*"
r, _ := regexp.Compile(keystr)
match := r.FindString(body)
keyValMatch := strings.Split(match, ":")
return strings.ReplaceAll(keyValMatch[1], "\"", "")
}
Regarding the given pattern, I didn't test all case, but it's scanning for a string like this
double quote, the name of the key, double quote, an semicolon and any char sequence except "," ";" "}" "]" (so basically anything that could close a key value pair in the syntax of json)
Example:
jsonResp := "{\"foo\":\"bar\"}"
value := extractValue(jsonResp, "foo")
fmt.Println(value)
would simple return bar
The main advantage I see, that you don't need to care about the structure of the JSON-response, but go just for the value you need by key.
Note: I think it's only possible to grab the value of the first matched key. But you can always modify the method. It just makes use of the regex-technology.