簡體   English   中英

從 json 步驟精確歷史 aws 創建數據框

[英]Create data frame from json step exaction history aws

我有 JSON 格式的步驟 function 執行歷史

[{
    "timestamp": "2022-07-18T13:03:03.346000+00:00",
    "type": "ExecutionFailed",
    "id": 3,
    "previousEventId": 2,
    "executionFailedEventDetails": {
        "error": "States.Runtime",
        "cause": "An error occurred while executing the state 'Workflow Choice state' (entered at the event id #2). Invalid path '$.contributor_id': The choice state's condition path references an invalid value."
    }
}, {
    "timestamp": "2022-07-18T13:03:03.306000+00:00",
    "type": "ChoiceStateEntered",
    "id": 2,
    "previousEventId": 0,
    "stateEnteredEventDetails": {
        "name": "Workflow Choice state",
        "input": "{\n    \"Comment\": \"Insert your JSON here\"\n}",
        "inputDetails": {
            "truncated": false
        }
    }
}, {
    "timestamp": "2022-07-18T13:03:03.252000+00:00",
    "type": "ExecutionStarted",
    "id": 1,
    "previousEventId": 0,
    "executionStartedEventDetails": {
        "input": "{\n    \"Comment\": \"Insert your JSON here\"\n}",
        "inputDetails": {
            "truncated": false
        },
        "roleArn": "arn:aws:iam::asdfg:role/step-all"
    }
}]

我們想創建一個如下所示的視圖在此處輸入圖像描述

問題是我無法將 executionFailedEventDetails、stateEnteredEventDetails、executionStartedEventDetails 創建為新行。 它只出現在第一行。

Step列是狀態中的名稱EnteredEventDetails

這就是我正在做的

import json

import pandas as pd
from tabulate import tabulate

raw = r"""[{
    "timestamp": "2022-07-18T13:03:03.346000+00:00",
    "type": "ExecutionFailed",
    "id": 3,
    "previousEventId": 2,
    "executionFailedEventDetails": {
        "error": "States.Runtime",
        "cause": "An error occurred while executing the state 'Workflow Choice state' (entered at the event id #2). Invalid path '$.contributor_id': The choice state's condition path references an invalid value."
    }
}, {
    "timestamp": "2022-07-18T13:03:03.306000+00:00",
    "type": "ChoiceStateEntered",
    "id": 2,
    "previousEventId": 0,
    "stateEnteredEventDetails": {
        "name": "Workflow Choice state",
        "input": "{\n    \"Comment\": \"Insert your JSON here\"\n}",
        "inputDetails": {
            "truncated": false
        }
    }
}, {
    "timestamp": "2022-07-18T13:03:03.252000+00:00",
    "type": "ExecutionStarted",
    "id": 1,
    "previousEventId": 0,
    "executionStartedEventDetails": {
        "input": "{\n    \"Comment\": \"Insert your JSON here\"\n}",
        "inputDetails": {
            "truncated": false
        },
        "roleArn": "arn:aws:iam::asdfg:role/step-all"
    }
}]"""


data = json.loads(raw, strict=False)
data = pd.json_normalize(data)
# print(data.to_csv(), index=False)
print(tabulate(data, headers='keys', tablefmt='psql'))
data.to_csv('file.csv',encoding='utf-8', index=False)

output 是

+----+----------------------------------+--------------------+------+-------------------+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------+----------------------------------------+---------------------------------------------------+----------------------------------------+-------------------------------------------------------+----------------------------------------+
|    | timestamp                        | type               |   id |   previousEventId | executionFailedEventDetails.error   | executionFailedEventDetails.cause                                                                                                                                                                 | stateEnteredEventDetails.name   | stateEnteredEventDetails.input         |   stateEnteredEventDetails.inputDetails.truncated | executionStartedEventDetails.input     |   executionStartedEventDetails.inputDetails.truncated | executionStartedEventDetails.roleArn   |
|----+----------------------------------+--------------------+------+-------------------+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------+----------------------------------------+---------------------------------------------------+----------------------------------------+-------------------------------------------------------+----------------------------------------|
|  0 | 2022-07-18T13:03:03.346000+00:00 | ExecutionFailed    |    3 |                 2 | States.Runtime                      | An error occurred while executing the state 'Workflow Choice state' (entered at the event id #2). Invalid path '$.contributor_id': The choice state's condition path references an invalid value. | nan                             | nan                                    |                                               nan | nan                                    |                                                   nan | nan                                    |
|  1 | 2022-07-18T13:03:03.306000+00:00 | ChoiceStateEntered |    2 |                 0 | nan                                 | nan                                                                                                                                                                                               | Workflow Choice state           | {                                      |                                                 0 | nan                                    |                                                   nan | nan                                    |
|    |                                  |                    |      |                   |                                     |                                                                                                                                                                                                   |                                 |     "Comment": "Insert your JSON here" |                                                   |                                        |                                                       |                                        |
|    |                                  |                    |      |                   |                                     |                                                                                                                                                                                                   |                                 | }                                      |                                                   |                                        |                                                       |                                        |
|  2 | 2022-07-18T13:03:03.252000+00:00 | ExecutionStarted   |    1 |                 0 | nan                                 | nan                                                                                                                                                                                               | nan                             | nan                                    |                                               nan | {                                      |                                                     0 | arn:aws:iam::asdfg:role/step-all       |
|    |                                  |                    |      |                   |                                     |                                                                                                                                                                                                   |                                 |                                        |                                                   |     "Comment": "Insert your JSON here" |                                                       |                                        |
|    |                                  |                    |      |                   |                                     |                                                                                                                                                                                                   |                                 |                                        |                                                   | }                                      |                                                       |                                        |
+----+----------------------------------+--------------------+------+-------------------+-------------------------------------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+---------------------------------+----------------------------------------+---------------------------------------------------+----------------------------------------+-------------------------------------------------------+----------------------------------------+

詳細信息列第 5 列是動態的,就像我目前僅給出 3 個事件的示例的所有列一樣,但它可以 go 達到任意數量。

最終預期 Output

在此處輸入圖像描述

給定file.json

[{
    "timestamp": "2022-07-18T13:03:03.346000+00:00",
    "type": "ExecutionFailed",
    "id": 3,
    "previousEventId": 2,
    "executionFailedEventDetails": {
        "error": "States.Runtime",
        "cause": "An error occurred while executing the state 'Workflow Choice state' (entered at the event id #2). Invalid path '$.contributor_id': The choice state's condition path references an invalid value."
    }
}, {
    "timestamp": "2022-07-18T13:03:03.306000+00:00",
    "type": "ChoiceStateEntered",
    "id": 2,
    "previousEventId": 0,
    "stateEnteredEventDetails": {
        "name": "Workflow Choice state",
        "input": "{\n    \"Comment\": \"Insert your JSON here\"\n}",
        "inputDetails": {
            "truncated": false
        }
    }
}, {
    "timestamp": "2022-07-18T13:03:03.252000+00:00",
    "type": "ExecutionStarted",
    "id": 1,
    "previousEventId": 0,
    "executionStartedEventDetails": {
        "input": "{\n    \"Comment\": \"Insert your JSON here\"\n}",
        "inputDetails": {
            "truncated": false
        },
        "roleArn": "arn:aws:iam::asdfg:role/step-all"
    }
}]

正在做

import pandas as pd

df = pd.read_json('file.json')
df = df.melt(['timestamp', 'type', 'id', 'previousEventId'], var_name='step', value_name='details').dropna()
print(df.to_markdown(index=False))

Output(Markdown 對我來說是最容易展示的):

| timestamp                        | type               |   id |   previousEventId | step                         | details                                                                                                                                                                                                                                   |
|:---------------------------------|:-------------------|-----:|------------------:|:-----------------------------|:------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| 2022-07-18 13:03:03.346000+00:00 | ExecutionFailed    |    3 |                 2 | executionFailedEventDetails  | {'error': 'States.Runtime', 'cause': "An error occurred while executing the state 'Workflow Choice state' (entered at the event id #2). Invalid path '$.contributor_id': The choice state's condition path references an invalid value."} |
| 2022-07-18 13:03:03.306000+00:00 | ChoiceStateEntered |    2 |                 0 | stateEnteredEventDetails     | {'name': 'Workflow Choice state', 'input': '{\n    "Comment": "Insert your JSON here"\n}', 'inputDetails': {'truncated': False}}                                                                                                          |
| 2022-07-18 13:03:03.252000+00:00 | ExecutionStarted   |    1 |                 0 | executionStartedEventDetails | {'input': '{\n    "Comment": "Insert your JSON here"\n}', 'inputDetails': {'truncated': False}, 'roleArn': 'arn:aws:iam::asdfg:role/step-all'}                                                                                            |

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM