JSONB数组输出-Postgres 11.5

时间:2019-11-21 19:53:40

标签: json postgresql jsonb

我有此格式的JSONB字符串

{
    "RouteId": "90679754-89f5-48d7-99e1-5192bf0becf9",
    "Started": "2019-11-20T21:24:33.7294486Z",
    "RouteName": "ProcessRequestsAndPublishResponse",
    "MachineName": "5CG8134NJW-LA",
    "ChildProfiles": [
        {
            "ApiMethod": "ProcessApiRequest",
            "ExecuteType": null,
            "DurationMilliseconds": 2521.4,

        },
        {
            "ApiMethod": "PublishShipViaToQueue",
            "ExecuteType": null,
            "DurationMilliseconds": 0.6,
        }
    ],
    "DataBaseTimings": null,
    "DurationMilliseconds": 2522.6
}

如何获取这种格式的输出

| RouteName                             | Metrics               | Time    | TotalDuration   |
---------------------------------------------------------------------------------------------
| ProcessRequestsAndPublishResponse     | ProcessApiRequest     | 2521.4  | 2522.6          |  
| ProcessRequestsAndPublishResponse     | PublishShipViaToQueue | 0.6     | 2522.6          |  
---------------------------------------------------------------------------------------------

对此提供任何帮助

在存在不同数组的情况下,如何扩展此方法。对不起,对于JSONB世界来说还很陌生。

{
    "RouteId": "af2e9cba-11ae-43a9-813c-d24ea574ee62",
    "RouteName": "GenerateRequestAndPublishToQueue",
    "ChildProfiles": [
        {
            "ApiMethod": "PublishShipViaRequestToQueue",
            "DurationMilliseconds": 0.1,
        }
    ],
    "DataBaseTimings": [
        {
            "ExecuteType": "OpenAsync",
            "DurationMilliseconds": 0.1
        },
        {
            "ExecuteType": "Reader",
            "DurationMilliseconds": 72.1
        },
        {
            "ExecuteType": "Close",
            "DurationMilliseconds": 15.9
        }
    ],
    "DurationMilliseconds": 88.6
}

所需的输出是这样的

| RouteName                             | Metrics                          | Time    | TotalDuration |
--------------------------------------------------------------------------------------------------------
| GenerateRequestAndPublishToQueue      | PublishShipViaRequestToQueue     | 0.1     | 88.6          |  
| GenerateRequestAndPublishToQueue      | OpenAsync                        | 0.1     | 88.6          |  
| GenerateRequestAndPublishToQueue      | Reader                           | 72.1    | 88.6          |  
| GenerateRequestAndPublishToQueue      | Close                            | 15.9    | 88.6          |  
---------------------------------------------------------------------------------------------------------

1 个答案:

答案 0 :(得分:0)

您可以执行'xdg-settings'并使用lateral join将内部json数组扩展为内联表:

jsonb_to_recordset()

Demo on DB Fiddlde

routename                         | metrics               |   Time | totalduration
:-------------------------------- | :-------------------- | -----: | :------------
ProcessRequestsAndPublishResponse | ProcessApiRequest     | 2521.4 | 2522.6       
ProcessRequestsAndPublishResponse | PublishShipViaToQueue |    0.6 | 2522.6