繁体   English   中英

如何在 Gatling 中使用 jsonpath 表达式从 json 中提取正确的 id

[英]how to extract correct id from json with jsonpath Expression in Gatling

我正在使用 Gatling 来测试 API,我必须从 JSON 中提取一个 ID 并将其存储在一个变量中,但 Gatling 不支持这个: "$.[?(@.name == 'manage-account')].id"我怎样才能找到属于'manage-account'的ID?

JSON 看起来像这样:

    {
  "realmRoles" : [ {
    "id" : "2253a9b4-bc1e",
    "name" : "offline_access",
    "description" : "${role_offline-access}",
    "composite" : false,
    "clientRole" : false,
    "containerId" : "sp"
  }, {
    "id" : "313d800c",
    "name" : "ua_authorization",
    "description" : "${role_ua_authorization}",
    "composite" : false,
    "clientRole" : false,
    "containerId" : "sp"
  } ],
  "clientRoles" : [ {
    "client" : {
      "id" : "37e4837b-8e77",
      "name" : "account"
    },
    "roles" : [ {
      "id" : "48abb872-dc25",
      "name" : "manage-account-links",
      "description" : "${role_manage-account-links}",
      "composite" : false,
      "clientRole" : true,
      "containerId" : "37e4837b-8e77e"
    }, {
      "id" : "a1fadc14-30c18",
      "name" : "manage-account",
      "description" : "${role_manage-account}",
      "composite" : true,
      "clientRole" : true,
      "containerId" : "37e4837b-8e77"
    } ]
  }, {
    "client" : {
      "id" : "ef80d8e9-5f9b",
      "name" : "realm-management"
    },
    "roles" : [ {
      "id" : "d1e35e29-b26e",
      "name" : "manage-users",
      "description" : "${role_manage-users}",
      "composite" : false,
      "clientRole" : true,
      "containerId" : "ef80d8e9-5f93"
    } ]
  }]
  } ]
}

JsonPath 目前缺乏适当的规范,因此语法解释因一种实现而异。

在这里,您应该使用$..*[?(@.name == 'manage-account')].id :您想要递归下降 ( .. ) 并匹配任何与您的过滤器匹配的节点 ( * )。

如果您想停止遭受这些差异的困扰,我建议您改用 JMESPath,请参阅我们的博客 JMESPath 与 JsonPath 相比主要缺乏的是递归下降,它很方便,但性能较差。 您必须指定完整路径。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM