marshmallow-jsonapi icon indicating copy to clipboard operation
marshmallow-jsonapi copied to clipboard

Ordering JSON data

Open dontknowwat opened this issue 8 years ago • 5 comments

I am not sure why but attributes come first instead of id and type, example

        "data": [
{
  "attributes": {
    "Birthday": "1986-08-11",
    "address": "ASAP",
    "creation_date": "2015-09-22T18:03:20.950821+00:00",
    "email": "[email protected]",
    "is_active": true,
    "mobile": 98765544,
    "name": "Leo",
    "website": "http://leog.in"
  },
  "id": "1",
  "type": "users"
},

dontknowwat avatar Sep 23 '15 12:09 dontknowwat

Python dictionaries are unordered. I suppose we could use a OrderedDict by default, or perhaps if the ordered class Meta option is set. I may look into this within the next week or two; PRs always welcome. =)

sloria avatar Sep 23 '15 13:09 sloria

This would be a bit of an odd feature. JSON objects are inherently unordered... I guess this is just so stuff looks better to humans for display?

taion avatar Sep 23 '15 14:09 taion

I guess this is just so stuff looks better to humans for display?

Correct; renderers may want to respect order (l believe DRF's browse-able API does this). Given that marshmallow has and ordered option, I think this feature makes sense.

sloria avatar Sep 24 '15 00:09 sloria

@Leo-G See https://marshmallow.readthedocs.io/en/latest/quickstart.html#ordering-output.

scottwernervt avatar Apr 10 '18 14:04 scottwernervt

Thought this was working but it is not, we need to pass ordered argument to SchemaOpts. I can work on a pull request when the marshmallow 3.0 changes are finalized.

scottwernervt avatar Apr 10 '18 15:04 scottwernervt