jq: transform json результаты от отчетов о сёрфинге по каратэ, получающих статус более простых шагов - PullRequest
1 голос
/ 13 марта 2020

Мне было интересно, как можно преобразовать json с помощью jq, но я не совсем понял, как получить желаемый результат; начиная с этого контента (который я получаю из отчета о поиске от каратэ автоматизации тестирования):

Источник:

[
    {
        "line": 1,
        "elements": [
            {
                "line": 3,
                "name": "",
                "description": "",
                "type": "background",
                "keyword": "Background",
                "steps": [
                    {
                        "name": "url 'http:\/\/be-service.test.svc.cluster.local\/SVC_INTERFACE_V1'",
                        "result": {
                            "duration": 256885,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "*",
                        "line": 4,
                        "doc_string": {
                            "content_type": "",
                            "value": "10:54:12.945 karate.env",
                            "line": 4
                        }
                    }
                ]
            },
            {
                "line": 9,
                "name": "",
                "description": "",
                "id": "SoapEsbBackEnd1",
                "type": "scenario",
                "keyword": "Scenario",
                "steps": [
                    {
                        "name": "request",
                        "result": {
                            "duration": 92526141,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "Given",
                        "line": 11,
                        "doc_string": {
                            "content_type": "",
                            "value": "<soap:Envelope xmlns:soap=\"http:\/\/schemas.xmlsoap.org\/soap\/envelope\/\">\n   <\/soap:Envelope>",
                            "line": 11
                        }
                    },
                    {
                        "name": "header Content-Type = 'application\/soap+xml; charset=utf-8'",
                        "result": {
                            "duration": 1834255,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "And",
                        "line": 74
                    },
                    {
                        "name": "method post",
                        "result": {
                            "duration": 102112259,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "When",
                        "line": 76,
                        "doc_string": {
                            "content_type": "",
                            "value": "10:54:13.140 request:\n1 > POST http:\/\/be-service.test.svc.cluster.local\/SVC_INTERFACE_V1",
                            "line": 76
                        }
                    },
                    {
                        "name": "status 200",
                        "result": {
                            "duration": 49328,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "Then",
                        "line": 77
                    },
                    {
                        "name": "def envelope = response['soapenv:Envelope']",
                        "result": {
                            "duration": 591138,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "*",
                        "line": 79
                    },
                    {
                        "name": "def body = response['soapenv:Envelope']['_']['soapenv:Body']",
                        "result": {
                            "duration": 470207,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "*",
                        "line": 81
                    },
                    {
                        "name": "def result = body['NS1:monitoraggioJBossResponse']._",
                        "result": {
                            "duration": 354671,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "*",
                        "line": 83
                    },
                    {
                        "name": "def esito = result.esito",
                        "result": {
                            "duration": 296656,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "*",
                        "line": 87
                    },
                    {
                        "name": "match esito == 'ERR'",
                        "result": {
                            "duration": 249327,
                            "status": "passed"
                        },
                        "match": {
                            "location": "karate",
                            "arguments": []
                        },
                        "keyword": "*",
                        "line": 89
                    }
                ],
                "tags": [
                    {
                        "name": "@esb",
                        "line": 6
                    },
                    {
                        "name": "@esb_svc_monitoraggio_ko",
                        "line": 6
                    },
                    {
                        "name": "@ko",
                        "line": 6
                    }
                ]
            }
        ],
        "name": "features\/esb-be1.feature",
        "description": "esb be first request",
        "id": "esb-be-first-request",
        "keyword": "Feature",
        "uri": "features\/esb-be1.feature"
    }
]

Что я хочу получить:

    [
        { "name":  "url 'http://be-service.test.svc.cluster.local/SVC_INTERFACE_V1'" , "status": "passed" },
        { "name":  "request" , "status": "passed" },
        { "name":  "header Content-Type = 'application/soap+xml; charset=utf-8'" , "status": "passed" },
        { "name":  "method post" , "status": "passed" },
        { "name":  "status 200" , "status": "passed" },
        { "name":  "def envelope = response['soapenv:Envelope']" , "status": "passed" },
        { "name":  "def body = response['soapenv:Envelope']['_']['soapenv:Body']" , "status": "passed" },
        { "name":  "def result = body['NS1:monitoraggioJBossResponse']._" , "status": "passed" },
        { "name":  "def esito = result.esito" , "status": "passed" },
        { "name":  "match esito == 'ERR'" , "status": "passed" }
    ]

То, что я пробовал с тех пор:

$ cat $JSON_FILE | jq '.[].elements[].steps[].name,.[].elements[].steps[].result.status'                                                   
"url 'http://be-service.test.svc.cluster.local/SVC_INTERFACE_V1'"
"request"
"header Content-Type = 'application/soap+xml; charset=utf-8'"
"method post"
"status 200"
"def envelope = response['soapenv:Envelope']"
"def body = response['soapenv:Envelope']['_']['soapenv:Body']"
"def result = body['NS1:monitoraggioJBossResponse']._"
"def esito = result.esito"
"match esito == 'ERR'"
"passed"
"passed"
"passed"
"passed"
"passed"
"passed"
"passed"
"passed"
"passed"
"passed"

1 Ответ

2 голосов
/ 13 марта 2020

В моем ответе используется то, что в руководстве описывается как «краткий синтаксис» для построения объекта . Создает два объекта, а затем складывает их вместе. Квадратные скобки в начале и в конце помещают выходные данные в массив.

jq '[.[].elements[].steps[]|{name} + (.result|{status})]'

enter image description here

Это эквивалентно тому, что оно стоит. Определение map(x) в руководстве объясняет, почему.

jq 'map(.elements[].steps[]|{name} + (.result|{status}))'

ic

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...