【问题标题】:Select random name from the JSON response in JMeter从 JMeter 中的 JSON 响应中选择随机名称
【发布时间】:2021-10-04 21:50:11
【问题描述】:

在响应正文中,我有以下 JSON,其中包含 id 和作为子元素的名称,我想在每次运行测试时选择随机名称及其对应的 id,该怎么做?

可能是通过后处理器或 json 提取器,我是 JMeter 的新手。

 {
    "entities": {
        "chairs": {
            "a99a921e-9ced-43eb-8521-a8e972c5afee": {
                "id": "a99a921e-9ced-43eb-8521-a8e972c5afee",
                "accountId": "02a39418-2936-4696-9f3f-02f503128ac7",
                "description": null,
                "isActive": true,
                "pmsId": "5079767850816079897",
                "name": "PranayOP",
                "createdAt": "2021-03-18T17:46:53.253Z",
                "updatedAt": "2021-03-18T17:46:53.253Z",
                "deletedAt": null
            },
            "eb125d3a-739f-479c-b803-dbffb6a871d0": {
                "id": "eb125d3a-739f-479c-b803-dbffb6a871d0",
                "accountId": "02a39418-2936-4696-9f3f-02f503128ac7",
                "description": null,
                "isActive": true,
                "pmsId": "-3332289498024566266",
                "name": "OVERFLOW",
                "createdAt": "2021-03-16T00:42:35.115Z",
                "updatedAt": "2021-03-16T00:42:35.115Z",
                "deletedAt": null
            },
            "36891051-1edf-46ea-a167-68d39a8a70f5": {
                "id": "36891051-1edf-46ea-a167-68d39a8a70f5",
                "accountId": "02a39418-2936-4696-9f3f-02f503128ac7",
                "description": null,
                "isActive": true,
                "pmsId": "-4453494259576621766",
                "name": "OP55",
                "createdAt": "2021-03-20T02:19:00.494Z",
                "updatedAt": "2021-03-20T02:19:00.494Z",
                "deletedAt": null
            },
            "85a2564e-7cd1-4e69-be5d-a979b5e56cd5": {
                "id": "85a2564e-7cd1-4e69-be5d-a979b5e56cd5",
                "accountId": "02a39418-2936-4696-9f3f-02f503128ac7",
                "description": null,
                "isActive": true,
                "pmsId": "8283273375660346038",
                "name": "OP02",
                "createdAt": "2021-03-16T00:42:34.832Z",
                "updatedAt": "2021-03-16T00:42:34.832Z",
                "deletedAt": null
            }
        }
    },
    "result": [
        "a99a921e-9ced-43eb-8521-a8e972c5afee",
        "eb125d3a-739f-479c-b803-dbffb6a871d0",
        "36891051-1edf-46ea-a167-68d39a8a70f5",
        "85a2564e-7cd1-4e69-be5d-a979b5e56cd5"
    ]
}

【问题讨论】:

    标签: jmeter


    【解决方案1】:
    1. JSON JMESPath Extractor 添加为返回上述 JSON 的请求的子项

    2. 配置如下:

      这里是 JMESPath 查询的文本表示以防万一:

      entities.chairs.*.[name,id]
      
    3. 就是这样,每次都会得到["OP55","36891051-1edf-46ea-a167-68d39a8a70f5"]形式的随机name/id组合

      在需要的地方可以引用${nameAndId}

    更多信息:The JMeter JSON JMESPath Extractor and Assertion: A Guide

    【讨论】:

    • 谢谢 Dimitri,但结果是 ["OP55","36891051-1edf-46ea-a167-68d39a8a70f5"],如何仅获取值?尝试使用 ${},但我只想要 OP55 和 36891051-1edf-46ea-a167-68d39a8a70f5
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-08-31
    • 1970-01-01
    • 2018-01-13
    • 2020-12-01
    • 2019-06-10
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多