Opened 7 months ago

Closed 7 months ago

Last modified 3 months ago

#36139 closed New feature (wontfix)

Allow expressing JSONObject keys dynamically, as well as explicit pair-values

Reported by: john-parton Owned by: john-parton
Component: Database layer (models, ORM) Version: dev
Severity: Normal Keywords:
Cc: Triage Stage: Unreviewed
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description (last modified by john-parton)

Currently there is no way to build a JSON object in the database using the JSONObject function where the keys are anything but static keys.

This is not a limitation of any of the backends that support JSON objects.

If you could specify the items of a JSONObject positionally, you get the benefit of being able to construct objects with non-static keys.

As an example esoteric usage, if you're trying to get the database to emit valid objects for the Elasticsearch DSL, you would almost certainly need to be able to build objects with non-static keys.

For example https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-term-query.html

The key "user.id" refers to a field in elastic.

See some discussion here: https://github.com/django/django/pull/19097#issuecomment-2611270246

I'll throw together a patch.

Change History (9)

comment:1 by john-parton, 7 months ago

Pull request/patch here: https://github.com/django/django/pull/19101

Not too bad for the actual code change, just a few lines. Several tests added and expanded the Usage section of the documentation sufficiently to cover the various use cases.

comment:2 by john-parton, 7 months ago

Has patch: set

comment:3 by Antoliny, 7 months ago

Owner: set to john-parton
Status: newassigned

comment:4 by Sarah Boyce, 7 months ago

Resolution: wontfix
Status: assignedclosed

Hi John, thank you for raising this!

As this is a new feature request, can you propose and discuss the idea on the Django Forum to gain consensus?
I can see that Simon said he felt this was a less useful feature than for JSONArray (refs #36134), so we should confirm that there is a need for this feature from the community before working on it.

I'll close the ticket for now, but if the community agrees with the proposal, please return to this ticket and reference the forum discussion so we can re-open it. For more information, please refer to the documented guidelines for requesting features.

comment:5 by john-parton, 7 months ago

Note that Simon said he felt that ON NULL ABSENT for JSONObject was less useful. This doesn't directly enable that functionality, as I mostly agree.

This is specifically for building JSON objects where keys are dynamic expressions, which Simon didn't comment on.

comment:7 by john-parton, 7 months ago

Description: modified (diff)

comment:8 by john-parton, 7 months ago

I've modified the description of this ticket to remove any references to ABSENT ON NULL and to remove any reasoning about that. Just straightforward describe just this one feature.

comment:9 by john-parton, 3 months ago

Coming back to this briefly.

The discussion was not particularly productive, but a specific use case of generating bits of Elasticsearch documents or queries was mentioned: https://forum.djangoproject.com/t/ability-to-specify-keys-for-jsonobject-using-arbitrary-expressions/38559/4

The elasticsearch query language is a subset of JSON where the specific field is represented as a key in a json object. Documentation: https://www.elastic.co/docs/explore-analyze/query-filter/languages/querydsl#query-filter-context-ex

If you have an EAV table and want to directly generate JSON documents where the keys correspond to the entities in your table, you cannot do that with the current JSONObject implementation. I give some reasoning here: https://forum.djangoproject.com/t/ability-to-specify-keys-for-jsonobject-using-arbitrary-expressions/38559/3

(I personally don't like EAV tables and would suggest storing extended attribute directly as json documents on rows, but you might have a legacy table design that prevents you from doing that.)

We can keep this wontfix, just wanted to come back and summarize, although I am interested in SImon's opinion. (Not sure how to summon him. A particular favorite food perhaps?)

Note: See TracTickets for help on using tickets.
Back to Top