[英]PSQL: How to extract information and collect all values in a specific column successfully
我在一個場景中苦苦掙扎,除了其中一列之外,我有多行重復相同的信息:
{
"common_name": "www.leagueoflegends.com",
"issuer_name": "C=US, O=GeoTrust Inc., CN=GeoTrust SSL CA - G3",
"not_before": "2016-03-24T00:00:00",
"not_after": "2017-03-24T23:59:59",
"key_algorithm": "RSA",
"key_size": 2048,
"serial_number": "\\x5cbeb7904e749cd466f1167bcd922ef0",
"signature_hash_algorithm": "SHA-256",
"signature_key_algorithm": "RSA",
"subject_name": "C=US, ST=California, L=Los Angeles, O=\"Riot Games, Inc.\", CN=www.leagueoflegends.com",
"name": "\\x3075310b3009060355040613025553311330110603550408130a43616c69666f726e6961311430120603550407140b4c6f7320416e67656c657331193017060355040a141052696f742047616d65732c20496e632e3120301e060355040314177777772e6c65616775656f666c6567656e64732e636f6d",
"alt_names": "bertha.leagueoflegends.com"
},
{
"common_name": "www.leagueoflegends.com",
"issuer_name": "C=US, O=GeoTrust Inc., CN=GeoTrust SSL CA - G3",
"not_before": "2016-03-24T00:00:00",
"not_after": "2017-03-24T23:59:59",
"key_algorithm": "RSA",
"key_size": 2048,
"serial_number": "\\x5cbeb7904e749cd466f1167bcd922ef0",
"signature_hash_algorithm": "SHA-256",
"signature_key_algorithm": "RSA",
"subject_name": "C=US, ST=California, L=Los Angeles, O=\"Riot Games, Inc.\", CN=www.leagueoflegends.com",
"name": "\\x3075310b3009060355040613025553311330110603550408130a43616c69666f726e6961311430120603550407140b4c6f7320416e67656c657331193017060355040a141052696f742047616d65732c20496e632e3120301e060355040314177777772e6c65616775656f666c6567656e64732e636f6d",
"alt_names": "battlegrounds.ru.leagueoflegends.com"
},
如您所見,唯一返回不同內容的字段是alt_names
。
我想知道是否有可能獲得數組中的所有alt_names
值,然后返回一行與剩余的列,為alt_names
附加數組。
最終我想將查詢輸出格式化為 JSON,如下所示:
[
{
"common_name": "www.leagueoflegends.com",
"issuer_name": "C=US, O=GeoTrust Inc., CN=GeoTrust SSL CA - G3",
"not_before": "2016-03-24T00:00:00",
"not_after": "2017-03-24T23:59:59",
"key_algorithm": "RSA",
"key_size": 2048,
"serial_number": "\\x5cbeb7904e749cd466f1167bcd922ef0",
"signature_hash_algorithm": "SHA-256",
"signature_key_algorithm": "RSA",
"subject_name": "C=US, ST=California, L=Los Angeles, O=\"Riot Games, Inc.\", CN=www.leagueoflegends.com",
"name": "\\x3075310b3009060355040613025553311330110603550408130a43616c69666f726e6961311430120603550407140b4c6f7320416e67656c657331193017060355040a141052696f742047616d65732c20496e632e3120301e060355040314177777772e6c65616775656f666c6567656e64732e636f6d",
"alt_names": "www.leagueoflegends.com", "xxx", "yyy", "zzz", ...
}
]
有人可以在這里幫助我嗎?
編輯:添加更多信息:
這是我的查詢目前的樣子:
SELECT array_to_json(array_agg(row_to_json(t)))
FROM(
SELECT
x509_commonName(c.certificate) as common_name,
x509_issuerName(c.certificate) as issuer_name,
x509_notBefore(c.certificate) as not_before,
x509_notAfter(c.certificate) as not_after,
x509_keyAlgorithm(c.certificate) as key_algorithm,
x509_keySize(c.certificate) as key_size,
x509_serialNumber(c.certificate) as serial_number,
x509_signatureHashAlgorithm(c.certificate) as signature_hash_algorithm,
x509_signatureKeyAlgorithm(c.certificate) as signature_key_algorithm,
x509_subjectName(c.certificate) as subject_name,
x509_name(c.certificate) as name,
x509_altNames(c.certificate) as alt_names
FROM certificate c
WHERE c.id = '$1') t
不幸的是,我不能將array_agg
與函數返回的結果一起使用,因此我正在尋找另一種選擇。
另外,我不希望將其存儲到另一列中,相反,我只想接收 JSON 格式的輸出並通過 Python 上的腳本對其進行處理。
謝謝
所以我最終解決了這個問題,如下所示:
SELECT row_to_json(u)
FROM (
SELECT
x509_commonName(c.certificate),
x509_issuerName(c.certificate) as issuer_name,
x509_notBefore(c.certificate) as not_before,
x509_notAfter(c.certificate) as not_after,
x509_keyAlgorithm(c.certificate) as key_algorithm,
x509_keySize(c.certificate) as key_size,
x509_serialNumber(c.certificate) as serial_number,
x509_signatureHashAlgorithm(c.certificate) as signature_hash_algorithm,
x509_signatureKeyAlgorithm(c.certificate) as signature_key_algorithm,
x509_subjectName(c.certificate) as subject_name,
x509_name(c.certificate) as name,
(
SELECT array_to_json(ARRAY(SELECT x509_altNames(c.certificate)
FROM certificate c
WHERE c.id = '$1'))
)
FROM certificate c
WHERE c.id = '$1'
) u
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.