1

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?

iqstatic
  • 2,322
  • 3
  • 21
  • 39
  • This seems to be a simple lexical ordering of the propery names. Jackson would support this via the [`JsonPropertyOrder` annotation](https://fasterxml.github.io/jackson-annotations/javadoc/2.7/index.html?com/fasterxml/jackson/annotation/JsonPropertyOrder.html), e.g. `@JsonPropertyOrder(alphabetic=true)`. – Thomas May 27 '21 at 10:25
  • The default value for alphabetic is false so it should not play a role here. Moreover, if that behavior is of the code, why postman and swagger outputs are different? – iqstatic May 27 '21 at 10:44
  • 1
    Sure, the default would be false but this is how it could be controlled/influenced from the producer side. Any consumer like swagger ui or postman can do their own reordering (e.g. like what Stefan answered). After all JSON doesn't impose any property ordering so you can't rely on that. – Thomas May 27 '21 at 11:09

1 Answers1

0

Probably just a Swagger feature. See https://swagger.io/docs/open-source-tools/swagger-ui/usage/configuration/

For readability, parameters are grouped by category and sorted alphabetically.

Stefan
  • 2,395
  • 4
  • 15
  • 32
  • Possible. And I think similar behavior is implemented by some of the online JSON viewers as well. But the thing is my API supports sorting behavior in which case the order is important and needs to be retained and hence this is undesirable. – iqstatic May 27 '21 at 11:05
  • Please take a look at the top answer to this question: https://stackoverflow.com/questions/3948206/json-order-mixed-up (and then go for a JSON Array) – Stefan May 27 '21 at 11:20