如何在Postman中验证JSON属性值

如何在Postman中验证JSON属性值,postman,Postman,我想验证测试脚本中的角色名。如果JSON数据有一个数组值,我怎么做 这是我的JSON对象: { "status": 200, "message": "Operation completed successfully.", "data": { "response": { "links": [ { &q

我想验证测试脚本中的角色名。如果JSON数据有一个数组值,我怎么做

这是我的JSON对象:

{
"status": 200,
"message": "Operation completed successfully.",
"data": {
    "response": {
        "links": [
            {
                "rel": "self",
                page=0&size=20"
            }
        ],
        "content": [
            {
                "roleEntityId": 7,
                "roleName": "Content Creator",
                "description": "Content Creator",
                "roleId": "RID30392625",
                "createdDate": "2020-07-15",
                "links": []
            }
         }
    }
}
我试过这个

pm.test("Verify the Role name ", function () {
    var jsonData = pm.response.json();
    pm.expect(jsonData.Content["roleName"]).to.eql("Content Creator");
   
});

考虑到示例响应,您的测试需要如下所示:

pm.test("Verify the Role name ", function () { 
    var jsonData = pm.response.json(); 
    pm.expect(jsonData.data.response.content[0].roleName).to.eql("Content Creator")
})

您对该值的引用不正确。
jsonData
变量将为您提供所有响应,您将不得不向下钻取JSON以获得
键<代码>内容
也是一个数组,因此您需要添加
[0]
,以指定所需数组中的对象。

鉴于示例响应,您的测试需要如下所示:

pm.test("Verify the Role name ", function () { 
    var jsonData = pm.response.json(); 
    pm.expect(jsonData.data.response.content[0].roleName).to.eql("Content Creator")
})
您对该值的引用不正确。
jsonData
变量将为您提供所有响应,您将不得不向下钻取JSON以获得
content
也是一个数组,因此您需要添加
[0]
来指定要在数组中添加的对象