Unmarshal dynamic JSON Golang

81 views Asked by At

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!

1

There are 1 answers

0
Masudur Rahman On

You could define a defined structure for the IndividualStatus and use as following.

type IndividualStatus struct {
    IsRunning    bool `json:"is_running"`
    VersionStats []struct {
        Digit       string `json:"digit"`
        ExpectedNum int    `json:"expected_num"`
        CurrentNum  int    `json:"current_num"`
        Type        string `json:"type"`
    } `json:"version-stats"`
    Data struct {
        Digit       string `json:"digit"`
        ExpectedNum int    `json:"expected_num"`
        CurrentNum  int    `json:"current_num"`
    } `json:"_data"`
    Status string `json:"status"`
    Count  int    `json:"count"`
}

type APIResponse []struct {
    IndividualStatus map[string]IndividualStatus
}

You can unmarshal your json response to this APIResponse and access each one of IndividualStatus field.

You can further modularize internal fields in IndividualStatus struct if you want.