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

使用Newman运行Postman集合时,如何从JSON输入文件提取嵌套账号密码

提取嵌套迭代数据中的name和password并传入请求体

因为你的迭代数据是嵌套结构,无法直接用pm.iterationData.get("name")获取深层字段,需要逐层访问对象属性来提取:

步骤1:获取当前迭代的完整数据对象

先将迭代数据转为可操作的JS对象:

const currentIteration = pm.iterationData.toObject();

步骤2:逐层提取嵌套字段

根据你的数据结构,先拿到testcases数组的第一个测试用例,再从中提取Input里的name和password:

// 提取第一个测试用例(如果有多个用例,可根据需求调整索引)
const targetTestcase = currentIteration.testcases[0];
// 获取账号和密码
const userName = targetTestcase.Input.name;
const userPassword = targetTestcase.Input.password;

步骤3:将值传入请求体

有两种常用方式:

  • 方式一:存入环境变量,请求体中使用变量引用
    如果你的请求体里写了{{name}}和{{password}}占位符,直接把值存入环境变量即可:

    pm.environment.set("name", userName);
    pm.environment.set("password", userPassword);
    
  • 方式二:直接修改请求体内容
    若不想用环境变量,可直接替换请求体中的对应内容:

    const reqBody = pm.request.body.toJSON();
    // 假设请求体原始内容包含占位符,替换为实际值
    reqBody.raw = reqBody.raw.replace("{{name}}", userName).replace("{{password}}", userPassword);
    pm.request.body.update(reqBody);
    

注意事项

  • 如果testcases数组包含多个测试用例,可结合pm.info.iteration动态调整数组索引
  • 可添加空值判断避免报错,比如:
    if (currentIteration?.testcases?.length > 0) {
      // 执行提取逻辑
    }
    

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.13 17:54:55