格式化JSON Postgresql

时间:2017-01-09 12:05:15

标签: json postgresql

我使用内部联接来连接3个表,所有者,存储和计算机。 我试图从多个表中查看输出JSON,如下所示:

SELECT ow.*, st.*, ma.* 
FROM owner ow 
   INNER JOIN st.store ON ow.OwnerId = st.OwnerId 
   INNER JOIN machine ma ON ma.StoreId = st.StoreId;

我希望JSON的格式如下:

{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":[{
        "StoreId": "s3ss5",
        "Name": "Store1",
        "Code": "bla",
        "Machine":[{
            "MachineId": "axpeo",
            "Name": "Machine1",
            "Type": "type1"
            }]
        },
        {
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":[{
            "MachineId": "weds",
            "Name": "Machine2",
            "Type": "type2"
            },
            {
            "MachineId": "axdso",
            "Name": "Machine3",
            "Type": "type3"
            }]
        }]
}

但返回JSON的格式不是这样的 我正在使用PostgreSQL。

3 个答案:

答案 0 :(得分:1)

最简单(也可能是唯一合理)的方法是从表级别的单个记录构建JSON子文档,然后才能分层加入它们:

SELECT json_build_object('OwnerId', ownerid,
                         'Name', name,
                         'Store', stores)
FROM owner
JOIN (
    SELECT ownerid,
           json_agg(
               json_build_object('StoreId', storeid,
                                 'Name', name,
                                 'Code', code,
                                 'Machine', machines)) AS stores
    FROM store
    JOIN (
        SELECT storeid,
               json_agg(
                   json_build_object('MachineId', machineid,
                                     'Name', name,
                                     'Type', type)) AS machines
        FROM machine
        GROUP BY storeid) m USING (storeid)
    GROUP BY ownerid) s USING (ownerid);

答案 1 :(得分:0)

输出不是我想要的,但它更好......这是输出

[{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":{
        "StoreId": "s3ss5",
        "Name": "Store1",
        "Code": "bla",
        "Machine":{
            "MachineId": "axpeo",
            "Name": "Machine1",
            "Type": "type1"
            }
        }
},
{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":{
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":{
            "MachineId": "weds",
            "Name": "Machine2",
            "Type": "type2"
            }
        }

},
{
    "OwnerId": "1d2dd",
    "Name": "name test",
    "Store":{
        "StoreId": "ddf22",
        "Name": "Store2",
        "Code": "ble",
        "Machine":{
            "MachineId": "axdso",
            "Name": "Machine3",
            "Type": "type3"
            }
        }
}]

它不会像同一个数组一样从同一个商店加入机器

答案 2 :(得分:0)

对于格式化为JSON的一对多关系,请尝试如下操作:

SELECT "owner"."id",
    json_agg(DISTINCT "store".*) AS "stores", 
    json_agg(DISTINCT "machine".*) AS "machines"
FROM "owners"
INNER JOIN "stores"
ON "stores"."ownerId" = "owners"."id"
INNER JOIN "machines"
ON "machines"."storeId" = "stores"."id"
WHERE "owner" = 1
GROUP BY "owner"."id";