问题
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