简体   繁体   中英

GoLang parse inconsistent JSON

I am currently writing an application that queries a third party API. Previously when I have done this, I have done it the correct way and created a struct and unmarshalled the response string into the struct and accessed all the data that way. However that only works if the data structure is consistent.

I have the issue of trying to query an API where the structure is inconsistent. If the request was successful I get the response

{'status': 'ok', 'due_date': '2023-01-01', 'library': 'AIEHA1'}

but if it's unsuccessful, depending on the error type, I get different structures; some examples:

{'status': 'unauthorized', 'error': 'Field Bearer empty'}
{'status': 'not-found', 'error-details': {'type': 'file-not-found', 'file': '/index'}}

Obviously, I can pass this into a generic map but I was wondering what the proper practice is for something like this? Yes the API is terrible, yes sadly I have to use it.

One way of dealing with this is to have a structure containing all possible fields:

type apiResult struct {
   Status string
   DueDate string
   Error string
   ErrorDetails ErrorDetail
}

You unmarshal the API response, then process the API result structure to determine the actual return type.

Another way of doing it is to unmarshal into the expected struct, and then unmarshal again into an error struct based on status:

json.Unmarshal(result,&data)
if data.Status=="error" {
    json.Unmarshal(result,&errorStruct)
} else if data.Status=="not-found" {
   ... etc.
}

When the response is not an error, this has the benefit of unmarshalling only once to the target struct.

Usually APIs return HTTP status other than 2xx code when there is an error. If that is the case, you can look at the response code and unmarshal based on that.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM