0

Is there a convention for the order the properties in a JSON response appear?

For example, given the response:

{
  "name": "Someone",
  "age": 22,
  "country": "Some Country"
}

You can see the properties are not ordered in any particular way. One ordering that comes to mind might be alphabetically:

{
  "age": 22,
  "country": "Some Country",
  "name": "Someone"
}

I want to keep my API responses consistent, so is there any convention toward that matter?

nbkhope
  • 6,516
  • 2
  • 32
  • 53

2 Answers2

1

The order of the JSON properties shouldn't matter.

You can use alphabetical order or put similar properties next to each other if you think it's easier for a human being to read the JSON document. But there's no difference for a machine.


The RFC 7159 is one of the documents that define the JSON format. See what is says about objects (highlight is mine):

An object is an unordered collection of zero or more name/value pairs, where a name is a string and a value is a string, number, boolean, null, object, or array.

However the order matters for array elements (highlight is also mine):

An array is an ordered sequence of zero or more values.

cassiomolin
  • 101,346
  • 24
  • 214
  • 283
0

It does not matter because you are accessing them as object key: let value = objectname.keyname If you are looping (Object.keys(objectname).forEach((item) => {....), the order is not consistent crossbrowser (see this). So there is no reason to care about order aside from logical order when debugging (console.log order).

R_Ice
  • 504
  • 5
  • 7