You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在JMeter的JsonPathExtractor中提取关联id的复杂值

JMeter JsonPath Extractor关联提取id与对应字段的解决方案

我在JMeter的JsonPath Extractor中使用表达式$.data[0].materials[?(@.id)].reports[*].metadata['materialName','materialType'],目前能提取出materialName和materialType,但需要把对应的id和这两个字段关联起来提取,期望输出格式为:

id1, materialName1, materialType1
id1, materialName2, materialType2
id2, materialName3, materialType3
id2, materialName4, materialType4

修正后的返回示例JSON(原JSON存在语法错误,已调整为合法可解析格式):

{
  "data": [
    {
      "materials": [
        {
          "reports": [
            {
              "metadata": {
                "materialName": "materialName1",
                "materialType": "materialType1"
              },
              "xvalue": null,
              "yvalue": null
            },
            {
              "metadata": {
                "materialName": "materialName2",
                "materialType": "materialType2"
              },
              "xvalue": null,
              "yvalue": null
            }
          ],
          "someValue": "someValue1",
          "id": "id1"
        },
        {
          "reports": [
            {
              "metadata": {
                "materialName": "materialName3",
                "materialType": "materialType3"
              },
              "xvalue": null,
              "yvalue": null
            },
            {
              "metadata": {
                "materialName": "materialName4",
                "materialType": "materialType4"
              },
              "xvalue": null,
              "yvalue": null
            }
          ],
          "someValue": "someValue1",
          "id": "id2"
        }
      ]
    }
  ]
}

修正后的JsonPath表达式

要实现id与对应materialName、materialType的关联提取,需要遍历每个material对象,将其id与下属reports的metadata字段组合,可使用以下两种表达式:

1. 直接组合字段的写法

$.data[0].materials[*].reports[*].['@.id', @.metadata.materialName, @.metadata.materialType]

2. 对象映射写法(更易读,兼容多数JsonPath实现)

$.data[0].materials[*].reports[*].{id: @.id, materialName: @.metadata.materialName, materialType: @.metadata.materialType}

备选方案(若JMeter不支持上述语法)

如果JMeter的JsonPath Extractor对复杂语法支持有限,可分两步操作:

  • 第一步:用$.data[0].materials[*].id提取所有id,保存为变量(例如命名为ids)
  • 第二步:用原表达式提取materialName和materialType,再通过循环控制器,根据索引将对应位置的id与字段拼接组合,得到期望格式的输出

内容的提问来源于stack exchange,提问作者Jatin

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.16 13:15:21