如何在 SQLite 中连接 json 数组对象值

How to concat a json arrays objects values in SQLite

作为更大的 select 查询的一部分,我需要从 json 数组的对象中提取值作为逗号分隔的字符串。

我已经设法从 json 对象中获取 json 数组:

SELECT * FROM (SELECT json_extract(Site.Login, '$.Uris') FROM Site);

给出相同结果的第二个变体:

SELECT value FROM json_each(Site.Login), Site WHERE json_each.key = 'Uris';

单行测试给出了想要的结果:

SELECT group_concat(json_extract(value, '$.Uri')) as login_uri FROM json_each('[{"Uri":"https://cnn.com"},{"Uri":"https://bbc.com"},{"Uri":"https://reuters.com"}]');

我迷失在矩阵中。我试过各种方法组合上面的查询代码,但我没有取得任何进展。

Site.Login 单元格的示例。 Uri对象的数量可以从0到无限。

{
    "Uris": [
            {"Uri":"https://cnn.com"},
            {"Uri":"https://bbc.com"},
            {"Uri":"https://reuters.com"}
    ],
    "Username": "ghhhhhhhhhhhhhfgggggggggggggggg",
    "Password": "hgfhfghfghfgh",
    "PasswordRevisionDate": "2019-01-07T21:51:42.65Z",
    "Totp": "gffffffffffffffffffffffhhhhhhhhhhhhhhhfghghgfh",
    "Name": "hgfhfghfghfghfgh",
    "PasswordHistory": [
        {
            "Password": "ghfghfghfghfghfg",
            "LastUsedDate": "2019-01-07T21:51:42.65Z"
        }
    ]
}

站点的完整布局table:

CREATE TABLE "Site" (
"Id" varchar primary key not null ,
"FolderId" varchar ,
"UserId" varchar ,
"OrganizationId" varchar ,
"Name" varchar ,
"Notes" varchar ,
"Fields" varchar ,
"PasswordHistory" varchar ,
"Login" varchar ,
"Card" varchar ,
"Identity" varchar ,
"SecureNote" varchar ,
"Favorite" integer ,
"Edit" integer ,
"OrganizationUseTotp" integer ,
"RevisionDateTime" bigint ,
"Type" integer ,
"Data" varchar )

select 查询应该 return 一个名为 login_uri 的列包含提取的 json 数组对象值作为连接字符串: https://cnn.com,https://bbc.com,https://reuters.com

你非常接近!

SELECT group_concat(json_extract(j.value, '$.Uri')) AS login_uri
FROM site AS s
JOIN json_each(json_extract(s.login, '$.Uris')) AS j

给予

login_uri                                          
---------------------------------------------------
https://cnn.com,https://bbc.com,https://reuters.com

如果您希望 table 中的每一行给出一个结果行,而不是将整个 table 中的 all uris 连接在一起成为一个单个字符串,在末尾添加一个GROUP BY s.id