Re: [Django] #35718: json_array missing from django.db.models.functions (was: Some useful postgres functions for JSON are missing from contrib)

4 views
Skip to first unread message

Django

unread,
Sep 4, 2024, 4:22:12 PM9/4/24
to django-...@googlegroups.com
#35718: json_array missing from django.db.models.functions
----------------------------------+---------------------------------------
Reporter: john-parton | Owner: john-parton
Type: New feature | Status: assigned
Component: contrib.postgres | Version: dev
Severity: Normal | Resolution:
Keywords: | Triage Stage: Unreviewed
Has patch: 1 | Needs documentation: 1
Needs tests: 1 | Patch needs improvement: 1
Easy pickings: 0 | UI/UX: 0
----------------------------------+---------------------------------------
Changes (by john-parton):

* summary: Some useful postgres functions for JSON are missing from
contrib => json_array missing from django.db.models.functions


Old description:

> There's a large number of postgres-specific functions for manipulating
> json: https://www.postgresql.org/docs/current/functions-json.html
>
> In my projects for work, I have created several:
>
> * ~~jsonb_build_object~~ -- Actually already implemented as
> `django.db.models.functions.JSONObject`
> * jsonb_build_array
> * jsonb_array_elements
> * ~~jsonb_extract_path~~ Functionally equivalent to the `#>` operator
> * ~~jsonb_extract_path_text~~ Functionally equivalent to the `#>>`
> operator
> * jsonb_array_length
>
> There also a few operators that I implemented as django functions
>
> * jsonb || jsonb -> jsonb - Concatenates two jsonb values
> * jsonb - text -> jsonb - Deletes a key (and its value) from a JSON
> object
>
> It would be nice if these were included in `contrib.postgres.functions`,
> ideally in a submodule like `contrib.postgres.function.json` to allow
> room to expand.
>
> I know postgres's json support is a bit of test-bed for more fleshed out
> json support in general, but I would recommend just adding things as
> postgres-specific to limit the scope of the changes.
>
> I can open a basic pull request.

New description:

This ticket was originally opened with the intent for me to provide
implementations for a bunch of postgres functions, but it became clear
from discussion that the most important bit was to implement a non-
postgres-specific JSONArray function.

Original post:

There's a large number of postgres-specific functions for manipulating
json: https://www.postgresql.org/docs/current/functions-json.html

In my projects for work, I have created several:

* ~~jsonb_build_object~~ -- Actually already implemented as
`django.db.models.functions.JSONObject`
* jsonb_build_array
* jsonb_array_elements
* ~~jsonb_extract_path~~ Functionally equivalent to the `#>` operator
* ~~jsonb_extract_path_text~~ Functionally equivalent to the `#>>`
operator
* jsonb_array_length

There also a few operators that I implemented as django functions

* jsonb || jsonb -> jsonb - Concatenates two jsonb values
* jsonb - text -> jsonb - Deletes a key (and its value) from a JSON object

It would be nice if these were included in `contrib.postgres.functions`,
ideally in a submodule like `contrib.postgres.function.json` to allow room
to expand.

I know postgres's json support is a bit of test-bed for more fleshed out
json support in general, but I would recommend just adding things as
postgres-specific to limit the scope of the changes.

I can open a basic pull request.

--
--
Ticket URL: <https://code.djangoproject.com/ticket/35718#comment:10>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.
Reply all
Reply to author
Forward
0 new messages