Description
- Firstly I'm querying data from MongoDB with "providerName" and returning it in response.
- Here the order of object is not preserving.
How to reproduce
/*
Sample data in database
{
"_id" : ObjectId("5eb2c12ff21aca93ec9d8c79"),
"title" : "vmware",
"config_attributes" : {
"vsphere_username" : "",
"vsphere_password" : "",
"vsphere_server" : "",
"allow_unverified_ssl" : ""
}
}
*/
type Providers struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Title string `json:"title,omitempty" bson:"title,omitempty" binding:"required"`
ConfigAttributes map[string]string `json:"config_attributes" bson:"config_attributes,omitempty" binding:"required"`
}
// GetOneProvider : Fetch provider with name
func GetOneProvider(c *gin.Context) {
providerName := c.Param("provider_name")
var provider Providers
provider,err := db.GetProvider(providerName)
if err != nil {
c.JSON(http.StatusInternalServerError, gin.H{
"message": "Something went wrong",
})
return
}
c.JSON(http.StatusOK, provider)
}
Expectations
$ curl http://localhost:8000/providers/vmware
{
"_id" : ObjectId("5eb2c12ff21aca93ec9d8c79"),
"title" : "vmware",
"config_attributes" : {
"vsphere_username" : "",
"vsphere_password" : "",
"vsphere_server" : "",
"allow_unverified_ssl" : ""
}
}
Actual result
As you can see order of objects inside config_attributes
is not preserved (seems to be in alphabetical order)
$ curl http://localhost:8000/providers/vmware
{
"_id": "5eb2c12ff21aca93ec9d8c79",
"title": "vmware",
"config_attributes": {
"allow_unverified_ssl": "",
"vsphere_password": "",
"vsphere_server": "",
"vsphere_username": ""
}
}
so is there any way to preserve order of json objects in response?
Comment From: marchellodev
@Mohitp98 Did you find a solution?
Comment From: Mohitp98
@Mohitp98 Did you find a solution?
@marchellodev NO
Actually, there is no point in preserving order in the JSON object, after all, we'll access values using its corresponding key, so I've closed this issue.
By default, it is in alphabetical order.
Comment From: olliedean
Anyone figured this out?