首页 > 解决方案 > 使用正则表达式 Jmeter 从 Json 获取密钥

问题描述

我正在使用正则表达式,并试图从这个身体中获取 id。但只有成员列表中的 id,而不是已验证密钥中的 id。:) 澄清一下,我在 JMeter 中使用正则表达式提取器

{
  "id": "9c40ffca-0f1a-4f93-b068-1f6332707d02", //<--not this
  "me": {
    "id": "38a2b866-c8a9-424f-a5d4-93b379f080ce",  //<--not this
    "isMe": true,
    "user": {
      "verified": {
        "id": "257e30f4-d001-47b3-9e7f-5772e591970b"  //<--not this
      }
    }
  },
  "members": [
    {
      "id": "88a2b866-c8a9-424f-a5d4-93b379f780ce", //<--this
      "isMe": true,
      "user": {
        "verified": {
          "id": "223e30f4-d001-47b3-9e7f-5772e781970b"  //<--not this
        }
      }
    },
    {
      "id": "53cdc218-4784-4e55-a784-72e6a3ffa9bc", //<--this
      "isMe": false,
      "user": {
        "unverified": {
          "verification": "XYZ"
        }
      }
    }
  ]
}

目前我有这个正则表达式:

("id": )("[\w-]+")

但正如您在此处看到的,它返回每个 guid

关于如何继续的任何想法?

提前致谢。

标签: regexjmeter

解决方案


由于输入数据类型为 JSON,建议使用JMeter 的 JSON Path Extractor Plugin

添加后,使用

$.members[*].id

JSON 路径表达式,用于匹配文档中作为顶部节点id的每个值的所有值。members

如果您可能已经嵌套memebers,则可以使用它们全部

$..members[*].id

你可以在https://jsonpath.com/测试这些表达式,看一个测试:

在此处输入图像描述


推荐阅读