I have a simple rest API written in java springboot that produces the JSON output as shown in the following example:
{
"status": 0,
"data": {
"total": 351,
"offset": 0,
"limit": 10,
"info": {
"1010": {
"id": 1010,
"name": "John",
"age": 28
},
"1009": {
"id": 1009,
"name": "Philippe",
"age": 42
},
"1008": {
"id": 1008,
"name": "Nick",
"age": 22
},
"1007": {
"id": 1007,
"name": "Razor",
"age": 19
},
"1006": {
"id": 1006,
"name": "Marco",
"age": 67
},
"1005": {
"id": 1005,
"name": "Pablo",
"age": 19
},
"1004": {
"id": 1004,
"name": "Sheldon",
"age": 29
},
"1003": {
"id": 1003,
"name": "Hazel",
"age": 34
},
"1002": {
"id": 1002,
"name": "Penny",
"age": 44
},
"1001": {
"id": 1001,
"name": "Chris",
"age": 41
}
}
},
"timeStamp": "2021-05-26T15:13:41.022+0000 UTC"
}
When I test the API using swagger, the JSON gets sorted automatically based on the keys in the following way:
{
"status": 0,
"data": {
"total": 351,
"offset": 0,
"limit": 10,
"info": {
"1001": {
"id": 1001,
"name": "Chris",
"age": 41
},
"1002": {
"id": 1002,
"name": "Penny",
"age": 44
},
"1003": {
"id": 1003,
"name": "Hazel",
"age": 34
},
"1004": {
"id": 1004,
"name": "Sheldon",
"age": 29
},
"1005": {
"id": 1005,
"name": "Pablo",
"age": 19
},
"1006": {
"id": 1006,
"name": "Marco",
"age": 67
},
"1007": {
"id": 1007,
"name": "Razor",
"age": 19
},
"1008": {
"id": 1008,
"name": "Nick",
"age": 22
},
"1009": {
"id": 1009,
"name": "Philippe",
"age": 42
},
"1010": {
"id": 1010,
"name": "John",
"age": 28
}
}
},
"timeStamp": "2021-05-26T15:13:41.022+0000 UTC"
}
Also when I use an online JSON viewer to visualize the JSON output, the behavior remains the same. However, when I hit the API using Postman, the order is retained. Can some explain this behavior and how this can be controlled?