如何在邮递员中根据条件从 JSON 响应数组中提取变量?

how to extract variable from JSON response array on condition basis in postman?

[
    {
        "Value": "987654",
        "Type": "EF"
    },
    {
        "Value": "987159",
        "Type": "DE"
    },
    {
        "Value": "987789",
        "Type": "CD"
    },
    {
        "Value": "987456",
        "Type": "BC"
    },
    {
        "Value": "987123",
        "Type": "AB"
    }
]

我想根据Type取值,保存到环境变量,如果Type=CD,Value设置为环境变量CDValue=987789

你可以做到这一点。

在 Postman 的 Test 选项卡中:

const res = pm.response.json();
res.forEach((e) => {
    pm.environment.set(e.Type + "Value", e.Value);
})

结果: