【问题标题】:Verify String is displaying within Nested JSON using Postman使用 Postman 验证字符串是否显示在嵌套 JSON 中
【发布时间】:2022-01-18 17:10:11
【问题描述】:

我正在使用 Postman 来验证一些 API 调用,我已经通过其中一个端点,其响应如下所示,我需要确保在该 JSON 响应中:

[
   
    {
        "contact": {
            "id": "k72yk2iwrf",
            "firstName": "Francis",
            "lastName": "Abell",
            "title": "Translational Science Project Manager",
            "company": "Sensei",
            "email": "aa@aa.cpom",
            "fax": {},
            "businessAddress": {
                "line1": "road",
                "line2": "Street",
                "line3": "Suite 710",
                "city": "Boston",
                "country": "US",
                "postalCode": "02210",
                "state": "MA"
            },
            "businessPhone": {
                "number": "123-123-1234",
                "ext": ""
            },
            "homeAddress": {},
            "homePhone": {},
            "mobilePhone": {}
        },
        "registration": {
            "id": "104656",
            "badgeId": "9208113975",
            "eventId": "TESTLIBRA-10"
        }
    },
    {
        "contact": {
            "id": "w4c4f2i7l4",
            "firstName": "Francis",
            "lastName": "Abell",
            "title": "Translational Science Project Manager",
            "company": "Sensei",
            "email": "aa@aa.cpom",
            "fax": {},
            "businessAddress": {
                "line1": "road",
                "line2": "Street",
                "line3": "Suite 710",
                "city": "Boston",
                "country": "US",
                "postalCode": "02210",
                "state": "MA"
            },
            "businessPhone": {
                "number": "123-123-1234",
                "ext": ""
            },
            "homeAddress": {},
            "homePhone": {},
            "mobilePhone": {}
        },
        "registration": {
            "id": "104656",
            "badgeId": "6803424516",
            "eventId": "TESTLIBRA-10"
        }
    }
]

我可以确保 "eventId" 正在显示并且它正在显示 "TESTLIBRA-10" 值。 无论 JSON 响应有多长,它都可以验证该属性以及该属性的值是否正在显示。

【问题讨论】:

    标签: javascript postman postman-collection-runner


    【解决方案1】:

    我自己得到了答案,我所做的是:

    var jsonArrayData = pm.response.json();
    
    pm.test('EventID property is displaying throughout JSON', function(){
    jsonArrayData.each(function(eventID){
        pm.expect(eventID.registration).to.have.property("eventId")
    })
    })
    
    pm.test('Entered Libra EventID is entered', function(){
    jsonArrayData.each(function(eventID){
        pm.expect(eventID.registration.eventId).to.eql("TESTLIBRA-10")
    })
    })
    

    【讨论】:

    • 正如目前所写,您的答案尚不清楚。请edit 添加其他详细信息,以帮助其他人了解这如何解决所提出的问题。你可以找到更多关于如何写好答案的信息in the help center
    猜你喜欢
    • 2018-12-24
    • 2012-07-02
    • 2012-08-03
    • 2017-04-02
    • 1970-01-01
    • 2017-06-10
    • 2011-09-29
    • 1970-01-01
    • 2011-12-05
    相关资源
    最近更新 更多