I am trying to Unmarshal an API response which has a dynamic field :
API response :
[
{
"id": "3412345678",
"userName": "ichigo",
"sub": "ichi",
"opnum": "324.45",
"applyRoot": [
"Paradise"
],
"prm": [],
"requestNumber": "",
"individualStatus": {
"app-first": {
"is_running": false,
"version-stats": [
{
"digit": "1.23.341",
"expected_num": 0,
"current_num": 0,
"type": "queued"
}
],
"_data": {
"digit": "12-rated",
"expected_num": 0,
"current_num": 0
},
"status": "timeout",
"count": 1
},
"app-second": {
"is_running": false,
"version-stats": [
{
"digit": "1.23.341",
"expected_num": 0,
"current_num": 0,
"type": "queued"
}
],
"_data": {
"digit": "12-rated",
"expected_num": 0,
"current_num": 0
},
"status": "timeout",
"count": 1
}
}
}
]
The model i tried for Unmarshalling the json is :
type AutoGenerated []struct {
ID string `json:"id"`
UserName string `json:"userName"`
Sub string `json:"sub"`
Opnum string `json:"opnum"`
ApplyRoot []string `json:"applyRoot"`
Prm []interface{} `json:"prm"`
RequestNumber string `json:"requestNumber"`
IndividualStatus map[string]interface{} `json:"individualAppStatus"`
}
What would be the correct method to Unmarshal the Json and access the IndividualStatus values.
I tried following Unmarshal JSON with some known, and some unknown field names , in this case i have to delete the values as mentioned (the values i have to delete is large). Pls let me know the best way.
Thank you!
You could define a defined structure for the
IndividualStatusand use as following.You can unmarshal your
jsonresponse to thisAPIResponseand access each one ofIndividualStatusfield.You can further modularize internal fields in
IndividualStatusstruct if you want.