> On 16 May 2018 at 05:59, David G. Johnston <david.g.johnston@gmail.com> wrote:
> On Tuesday, May 15, 2018, reader 1001 <007reader@gmail.com> wrote:
>>
>> Yes, I realized it by now, thank you.
>> My question remains for hierarchical keys in a JSON document. If I have a
>> document like below, I clearly can extract key1 using the described rowtype
>> definition. How can I specify selected keys deeper in the document, e.g.
>> key3 and key5?
>> {
>> key1:value1,
>> key2: {
>> key3:value3},
>> key4:[
>> {
>> key5:value5
>> },
>> {
>> key6:value6
>> }
>> ]
>> }
>>
>
> I believe you would need a type for each subtree and apply the function
> multiple times with the result of one feeding the next.
Yes, you need to defined a type for each subtree, but as far as I can
tell it's not necessary to apply the function multiple times,
`jsonb_populate_record` can work with nested types, so it's enough
just to have every new type included in the previous one. I have this
simple example, it should be easy to adapt it for your case (although
the value extraction part looks a bit cumbersome):
create type key1 as (a text);
create type key2 as (b key1);
create type key3 as (c key2);
create type key4 as (d key3, e text);
select (((d).c).b).a, e
from jsonb_populate_record(null::key4,
'{"d": {"c": {"b": {"a": "nested"}}}, "e": "test"}');
a | e
--------+------
nested | test
(1 row)