I use Postgresql 10 and suppose, that I have the following table:
CREATE TABLE test_table(id int8 NOT NULL, data jsonb);
And inserted such values:
insert into test_table(id, data)
values (1, '{"external": [{"internal_1": "value_1", "internal_2": "some_value"}]}'),
(2, '{"external": [{"internal_1": "value_2", "internal_2": "another_value"}]}'),
(3, '{"external": [{"internal_1": "value_1", "internal_2": "other_value"}]}');
The table after all:
id| data |
--|-------------------------------------------------------------------------|
1 |{"external": [{"internal_1": "value_1", "internal_2": "some_value"}]} |
2 |{"external": [{"internal_1": "value_2", "internal_2": "another_value"}]} |
3 |{"external": [{"internal_1": "value_1", "internal_2": "other_value"}]} |
The problem is that I need to have all internal values as an array
I tried the following query:
select data -> 'external'
from test_table;
and the result is:
?column? |
-----------------------------------------------------------------|
[{"internal_1": "value_1", "internal_2": "some_value"}] |
[{"internal_1": "value_2", "internal_2": "another_value"}] |
[{"internal_1": "value_1", "internal_2": "other_value"}] |
But finally I need to have internal values as text array for each table row. Something like that:
?column? |
-------------------------------|
[value_1, some_value] |
[value_2, another_value] |
[value_1, other_value] |
Is it possible to write such query using postgres json functions and operators?
UPD!! Number of internal keys could be different (internal_1, ..., internal_n) and we don’t know in advance names of keys (in my case they are: internal_1 and internal_2)
internal_…(and if yes, what to do with them)?