The following PostgreSQL query produces an output in json format.
SELECT exampleColumn::jsonb FROM public."MyTable"
The output is as follows:
[
{
"testSettings":{
"www.test1.com":{
"IpAddress":[
{
"ipv4Addr":"192.168.0.1",
"myId":"myIdExample"
}
]
},
"www.try3.com":{
"IpAddress":[
{
"ipv4Addr":"192.168.0.5",
"myId":"myIdExample"
}
]
}
},
"testSettings":{
"www.test6.com":{
"IpAddress":[
{
"ipv4Addr":"192.168.0.7",
"myId":"myIdExample"
}
]
},
"www.try8.com":{
"IpAddress":[
{
"ipv4Addr":"192.168.0.4",
"myId":"myIdExample"
}
]
}
}
}
]
I'm aware that testSettings is not unique. However, values starting with 'www' may or may not be unique.
I want to get an output like this using this data:
| website | ipv4Addr |
|--------------- |------------- |
| www.test1.com | 192.168.0.1 |
| www.try3.com | 192.168.0.5 |
| www.test6.com | 192.168.0.7 |
| www.try8.com | 192.168.0.4 |
I tried to use the example below for this. However, when I typed a query as
SELECT exampleColumn ::jsonb
FROM public."MyTable" in values()
WITH c(j) AS (
values(SELECT exampleColumn ::json
FROM public."MyTable")
)
SELECT j->jsonb_object_keys(j)->>'www*'
FROM public."MyTable";
I got an error.
https://stackoverflow.com/a/48923352
It would be easier if the website names were the same. What could be our solutions for this issue? Thank you.
jsonbwill remove duplicate keys, so the secondtestSettingselement will be lost.