In backend I send http request to some third party site and retrieve some json data in response. Keys in the json response are not always same, but I have some idea of what they might be. For example:
Sending request to example.com/data/1
could bring the following:
{
"File.Trimmed": 54,
"Feature": "Generic"
}
While requesting to example.com/data/2
could bring the following:
{
"File.Trimmed": 83,
"Object.Notation": "Reverse"
}
My objective is to render html tables in the frontend using the key-value pairs in exact same order as they were in the response.
So the first table would be:
Column A | Column B |
---|---|
File.Trimmed | 54 |
Feature | Generic |
And the second table:
Column A | Column B |
---|---|
File.Trimmed | 83 |
Object.Notation | Reverse |
I have made the following struct to deal with this data:
type FileDetails struct {
FileTrimmed int `json:"File.Trimmed,omitempty"`
Feature string `json:"Feature,omitempty"`
ObjectNotation string `json:"Object.Notation,omitempty"`
}
// cannot use map[string]interface{} because that would destroy the order
var data FileDetails
err = json.NewDecoder(res.Body).Decode(&data)
At this point I am struggling how to send data
to the template and render the tables. It's possible to get the json tags (content for Column A) from the struct instance using reflect
, but is it possible to get field names like that in {{range}} loops in the template? If possible, then how?
Or is there any better way to achieve my main objective?