Opened 3 weeks ago
Last modified 3 weeks ago
#37349 assigned Cleanup/optimization
Add note about object key order in JSONField docs
| Reported by: | Lincoln | Owned by: | SnippyCodes |
|---|---|---|---|
| Component: | Documentation | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Lincoln | Triage Stage: | Accepted |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
Using JSONField with PostgreSQL, storing a dict in the field and then later retrieving it, will not preserve the key order,
because PostgreSQL jsonb type "does not preserve the order of object keys" (https://www.postgresql.org/docs/current/datatype-json.html).
This behavior can be surprising, because python dict does preserve key order, and storing/retrieving dicts with json.dumps/json.loads will preserve key order.
So, I am proposing to add a note to the JSONField docs explaining that key order may not be preserved. This could go in either the current "PostgreSQL users" note, or a new note. This probably depends on if the other DB backends preserve key order. I haven't checked if any of the others do.
Change History (5)
comment:1 by , 3 weeks ago
comment:2 by , 3 weeks ago
| Triage Stage: | Unreviewed → Accepted |
|---|
I think adding a link to the Postgres docs considering we already have a section on the jsonb vs json types makes sense. I am also ok with a small note about the key order.
I agree with David that we try not to document other systems, but as we already have a section on this, I am comfortable with making that section slightly more informative.
My suggestion would be:
-
docs/ref/models/fields.txt
a b To query ``JSONField`` in the database, see :ref:`querying-jsonfield`. 1508 1508 queried. PostgreSQL's ``json`` field is stored as the original string 1509 1509 representation of the JSON and must be decoded on the fly when queried 1510 1510 based on keys. The ``jsonb`` field is stored based on the actual structure 1511 of the JSON which allows indexing. The trade-off is a small additional cost 1512 on writing to the ``jsonb`` field. ``JSONField`` uses ``jsonb``. 1511 of the JSON, which allows indexing but doesn't preserve the order of object 1512 keys. The trade-off is a small additional cost on writing to the ``jsonb`` 1513 field. ``JSONField`` uses ``jsonb``. 1514 1515 For more details, see PostgreSQL's `documentation on JSON types 1516 <https://www.postgresql.org/docs/current/datatype-json.html>`_.
comment:3 by , 3 weeks ago
| Owner: | set to |
|---|---|
| Status: | new → assigned |
comment:4 by , 3 weeks ago
I will take this on! I will apply Sarah's suggested wording to docs/ref/models/fields.txt and ensure the paragraph is wrapped to 80 characters.
comment:5 by , 3 weeks ago
| Has patch: | set |
|---|
I believe the docs policy is not to document other systems, rather refer to them.
Other than key order there are other items that some folks may find interesting: