JsonPath:过滤器后获取父元素

时间:2018-12-20 15:07:59

标签: jsonpath json-path-expression

下面是我的json文档:

[
  {
    "line": 1,
    "elements": [
      {
        "before": [
          {
            "result": {
              "duration": 1363286,
              "status": "passed"
            },
            "match": {
              "location": "Hooks.init(Scenario)"
            }
          }
        ],
        "line": 4,
        "name": "List Size test",
        "description": "",
        "id": "test-list-size;list-size-test",
        "after": [
          {
            "result": {
              "duration": 82262,
              "status": "passed"
            },
            "match": {
              "location": "Hooks.closeAllwindows()"
            }
          },
          {
            "result": {
              "duration": 127317,
              "status": "passed"
            },
            "match": {
              "location": "Hooks.embedScreenShot(Scenario)"
            }
          }
        ],
        "type": "scenario",
        "keyword": "Scenario",
        "steps": [
          {
            "result": {
              "duration": 4170752,
              "status": "passed"
            },
            "line": 6,
            "name": "def test\u003d\u0027{     \\\"mytest\\\":[        {           \\\"test1\\\":\\\"abc\\\",         \\\"test2\\\":\\\"bcj\\\"      },      {           \\\"test1\\\":\\\"abc\\\",         \\\"test2\\\":\\\"bcj\\\"      }   ]}\u0027",
            "match": {
              "arguments": [
                {
                  "val": "test\u003d\u0027{     \\\"mytest\\\":[        {           \\\"test1\\\":\\\"abc\\\",         \\\"test2\\\":\\\"bcj\\\"      },      {           \\\"test1\\\":\\\"abc\\\",         \\\"test2\\\":\\\"bcj\\\"      }   ]}\u0027",
                  "offset": 4
                }
              ],
              "location": "APIStepDefs.def(StringBuilder)"
            },
            "keyword": "Given "
          },
          {
            "result": {
              "duration": 980309,
              "status": "passed"
            },
            "line": 7,
            "name": "I convert \"${test}\" to response",
            "match": {
              "arguments": [
                {
                  "val": "${test}",
                  "offset": 11
                }
              ],
              "location": "UtilsStepDefs.i_convert_to_htf_response(String)"
            },
            "keyword": "And "
          },
          {
            "result": {
              "error_message": "java.lang.AssertionError: JSON PATH mytest LISTSIZE validation is failed .  Actual ArrayList size is :: 2 Expected ArrayList size is :: 3\r\n\tat org.testng.Assert.fail(Assert.java:96)\r\n\tat com.ingenico.htf.generickeywords.assertions.JsonAssertions$13.performJSONAssertion(JsonAssertions.java:323)\r\n\tat com.ingenico.htf.generickeywords.client.apiclient.APIClient.assertJSONResponse(APIClient.java:335)\r\n\tat com.ingenico.htf.generickeywords.stepdefs.APIStepDefs.assert_JSON(APIStepDefs.java:127)\r\n\tat ✽.assert JSONPATH LISTSIZE(src/test/features/CheckListSize2.feature:8)\r\n",
              "duration": 1293311,
              "status": "failed"
            },
            "line": 8,
            "name": "assert JSONPATH LISTSIZE",
            "match": {
              "arguments": [
                {
                  "val": "LISTSIZE",
                  "offset": 16
                }
              ],
              "location": "APIStepDefs.assert_JSON(JsonAssertions,DataTable)"
            },
            "rows": [
              {
                "cells": [
                  "mytest",
                  "3"
                ]
              }
            ],
            "keyword": "Then "
          }
        ],
        "tags": [
          {
            "name": "@listsize"
          }
        ]
      }
    ],
    "name": "Test List size",
    "description": "",
    "id": "test-list-size",
    "keyword": "Feature",
    "uri": "src/test/features/CheckListSize2.feature",
    "tags": []
  }
]

我想拥有所有这些elements.id,其中elements [*]。steps [*]。result'失败'

我尝试了以下查询,但未返回任何结果。

$..elements[?(@.steps[*].result=="failed")].id

当前行为:Json列表为空[]

预期的行为:查询必须返回结果状态为“失败”的所有element.id。

例如: id ='test-list-size; list-size-test'

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

您使用哪个JSONPath评估程序?并且有最大步骤数吗?以下适用于jayway

$..elements[*][?(@.steps[0].result.status=='failed' || @.steps[1].result.status=='failed' || @.steps[2].result.status=='failed')].id

输出:

[
   "test-list-size;list-size-test"
]

但是对于无法消化不存在的步骤的评估者来说,这将失败[n]