0
Follow
2
View

Postgres JSONB function to key an array of objects, by a field

aidalaohuyou 注册会员
2023-01-25 11:46

Use the function:

create or replace function jsonb_key_by(jsonb, text)
returns jsonb language sql immutable as $$
    select jsonb_object_agg(elem->>$2, elem)
    from jsonb_array_elements($1) as arr(elem)
$$;

Test it in db<>fiddle.

About the Author

Question Info

Publish Time
2023-01-25 11:46
Update Time
2023-01-25 11:46