sql >> Base de Datos >  >> RDS >> PostgreSQL

Recuperación de elementos json con un nombre de clave específico de una estructura anidada compleja en postgres

Esta función recursiva extrae todos los atributos de un objeto jsonb complejo:

create or replace function jsonb_extract_all(jsonb_data jsonb, curr_path text[] default '{}')
returns table(path text[], value text)
language plpgsql as $$
begin
    if jsonb_typeof(jsonb_data) = 'object' then
        return query 
            select (jsonb_extract_all(val, curr_path || key)).*
            from jsonb_each(jsonb_data) e(key, val);
    elseif jsonb_typeof(jsonb_data) = 'array' then
        return query 
            select (jsonb_extract_all(val, curr_path || ord::text)).*
            from jsonb_array_elements(jsonb_data) with ordinality e(val, ord);
    else
        return query
            select curr_path, jsonb_data::text;
    end if;
end $$;

Ejemplo de uso:

with my_table(data) as (
select
    '{
        "$type": "a",
        "other": "x",
        "nested_object": {"$type": "b"},
        "array_1": [{"other": "y"}, {"$type": "c"}],
        "array_2": [{"$type": "d"}, {"other": "z"}]
    }'::jsonb
)

select f.*
from my_table
cross join jsonb_extract_all(data) f
where path[cardinality(path)] = '$type';

         path          | value 
-----------------------+-------
 {$type}               | "a"
 {array_1,2,$type}     | "c"
 {array_2,1,$type}     | "d"
 {nested_object,$type} | "b"
(4 rows)