var x_inboundbody map[string]interface{}|
_ = json.Unmarshal(Inboundbody, &x_inboundbody)|
I’ve a 3…4 stage deep construction, like a JSON construction (because of dynamic nature it’s not viable to pre create a struct), however it’s dynamic from report to report.
I do know that in my construction I’ve the next key’s from a root stage entities.overalScore.overallScore that I wish to entry.
I wish to extract the worth 60 as per under.
Can anybody assist.
G
The simplest manner is to map your jsonto a go struct (use JSON-to-Go: Convert JSON to Go immediately) .
In any other case it’s a must to bear in mind, every factor tahta you get from map is a struct{}, so it needs to be convert to the appropiate information kind. The code might be one thing like:
Inboundbody := []byte(json_string)
var x_inboundbody map[string]interface{}
_ = json.Unmarshal(Inboundbody, &x_inboundbody)
fmt.Println(x_inboundbody) // map[string]interface{}
responseBodyInterface := x_inboundbody["responseBody"] // interface{}
responseBodyMap := responseBodyInterface.(map[string]interface{}) // map[string]interface{}
fmt.Printf("responseBodyMap :%Tnpercentvn", responseBodyMap, responseBodyMap)
entitiesInterface := responseBodyMap["entities"] // interface{}
entitiesSlice := entitiesInterface.([]interface{}) // []interface{}
entitiesMap := entitiesSlice[0].(map[string]interface{}) // map[string]interface{}
fmt.Printf("entitiesMap :%Tnpercentvn", entitiesMap, entitiesMap)
overallScoreInterface := entitiesMap["overallScore"] // // interface{}
overallScoreMap := overallScoreInterface.(map[string]interface{})
fmt.Printf("overallScoreMap :%Tn%+vn", overallScoreMap, overallScoreMap)
overallScore := overallScoreMap["overallScore"]
fmt.Printf("overallScore :%Tnpercentvn", overallScore, overallScore)
HTH,
Yamil