Postman Testing - JSON - iterate over response with duplicate keys with different values

女生的网名这么多〃 提交于 2021-01-28 19:17:41

问题


I'm new to the postman testing and i have found this case. I have this response

{
    "company": [
        {
            "publicKey": "40",
            "mutkey": "13273811",
            "employee": [
                {
                    "publicKey": "3030",
                    "mutkey": "13415424",
                    "formattedName": "V Vierde",
                    "contract": [
                        {
                            "publicKey": "1",
                            "mutkey": "13415424",
                            "functionName": "***NO FUNCTION NAME FOUND***"
                        }
                    ]
                },
                {
                    "publicKey": "3040",
                    "mutkey": "13415426",
                    "formattedName": "V Vijfde",
                    "contract": [
                        {
                            "publicKey": "1",
                            "mutkey": "13415426",
                            "functionName": "***NO FUNCTION NAME FOUND***"
                        }
                    ]
                }
            ]
        }
    ]
}

I need to get into collection, f.e. an Array, every value for key functionName. Is there a way in Postman to do that?


回答1:


With this you can get each functionName:

const resBody = pm.response.json();

let employees = resBody.company[0].employee

let numberOfEmployees = employees.length

for (var i = 0; i < numberOfEmployees; i++){
    let functionName = employees[i].contract[0].functionName;
    console.log(functionName);
    // your code here
}


来源:https://stackoverflow.com/questions/63847129/postman-testing-json-iterate-over-response-with-duplicate-keys-with-differen

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!