【问题标题】:Can I pass a variable name in json path expression in jmeter json extractor?我可以在 jmeter json 提取器中的 json 路径表达式中传递变量名吗?
【发布时间】:2018-07-30 20:00:25
【问题描述】:

对于以下 json 响应 - 我在一个 json 提取器中提取所有 id 作为 flight_id。 现在我要提取上面得到的所有 flight_ids(123, 456) 的状态值。

为此,我在每个控制器下添加了另一个 json 提取器,我使用的 json 路径表达式是 -

$..data[*].[?(@.[".flights"][?(@.["__id"] == '${flight_id}')])].[".status" ]

但这似乎不起作用,我看不到调试采样器中返回的状态。 我不能在 json 路径表达式中传递变量名吗?

还有其他方法吗? 谢谢!

{
"count": 1,
"data": [
    {
        "__id": "ed6f709c-fc38-4b0e-9064-1170b1a0403d",
        "__r": 43,
        "__mb": "674a2597-07cb-46a2-a32c-e9bda1171b6d",
        "__m": "2018-01-24T21:27:03.689-05:00",
        ".auths": [],
        ".createdBy": "7f281748-16de-4f65-901c-61f1a0f2fed4",
        ".createdDate": "2017-12-04T21:45:37.592-05:00",
        ".currency": "VND",
        ".events": [],
        ".factsheets": [],
        ".flights": 
        [
            {
            "__id": "123",
            "__r": 43,
            "__mb": "7f281748-16de-4f65-901c-61f1a0f2fed4",
            "__m": "2017-12-11T09:18:09.755-05:00",
            ".latestAuthorization": null,
            ".latestAuthorizationStatus": null,
            ".status": "budgeted",
            ".subflights": [

            ],
            "addedValue.actual": null,
            "addedValue.booked": null,

          },
          {
            "__id": "456",
            "__r": 43,
            "__mb": "7f281748-16de-4f65-901c-61f1a0f2fed4",
            "__m": "2017-12-11T09:18:09.755-05:00",
            ".latestAuthorization": null,
            ".latestAuthorizationStatus": null,
            ".status": "actual",
            ".subflights": [

            ],
            "addedValue.actual": null,
            "addedValue.booked": null,

          }
        ]

【问题讨论】:

    标签: jmeter


    【解决方案1】:

    鉴于您提供:

    1. 只有部分 JSON
    2. JSON 格式不正确(航班之间缺少逗号)

    很难说哪里出了问题,但是如果有正确的 JSON 有效负载,例如:

    [
      {
        "__id": "123",
        "__r": 43,
        "__mb": "7f281748-16de-4f65-901c-61f1a0f2fed4",
        "__m": "2017-12-11T09:18:09.755-05:00",
        ".latestAuthorization": null,
        ".latestAuthorizationStatus": null,
        ".status": "budgeted",
        ".subflights": [
    
        ],
        "addedValue.actual": null,
        "addedValue.booked": null,
    
      },
      {
        "__id": "456",
        "__r": 43,
        "__mb": "7f281748-16de-4f65-901c-61f1a0f2fed4",
        "__m": "2017-12-11T09:18:09.755-05:00",
        ".latestAuthorization": null,
        ".latestAuthorizationStatus": null,
        ".status": "actual",
        ".subflights": [
    
        ],
        "addedValue.actual": null,
        "addedValue.booked": null,
    
      }
    ]   
    

    以及以下 JSON 路径查询:

    $..[?(@.__id == '${flight_id}')].[".status"]
    

    它工作正常并返回budgeted123actual456

    您可以使用View Results Tree 侦听器的JSON Path Tester 模式验证您的JSON 路径查询。如果需要,请参阅How to Debug your Apache JMeter Script 文章了解更多详细信息。

    【讨论】:

    • 谢谢@Dmitri。我已经更新了我的问题中的 json 格式,并且我正在使用这个 json 路径查询来获取 status 的值,但是我没有得到任何回报。 $..data[*].[".flights"][*].[?(@.__id == '${flight_id}')].[".status"]
    猜你喜欢
    • 2020-11-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-10-27
    • 1970-01-01
    • 1970-01-01
    • 2020-11-03
    相关资源
    最近更新 更多