英文:
How to iterate through postman JSON response and store it to global variable based on if else condition is met?
问题
Here is the translated code portion:
res = pm.response.json()
let users_len = res.data.users.length
for (let i = 0; i < users_len; i++) {
is_retail = res.data.users[i].isRetail;
if (is_retail == "true") {
pm.globals.set("UserId", res.data.users[i].id);
} else if (is_retail == "false") {
pm.globals.set("orgUserId", res.data.users[i].id);
}
}
This code attempts to loop through a JSON response, checking the "isRetail" parameter and setting the ID to a global variable based on the condition. The error you mentioned is likely due to an off-by-one error in the loop condition (using <=
instead of <
). The corrected code uses <
to iterate through the array correctly.
英文:
I have a array object in json response in which i want to loop through isRetail parameter and set the id to global variable if the condition is met.
Sample JSON response:
{
"status": 200,
"data": {
"users": [
{
"id": 95,
"isRetail": true
},
{
"id": 118,
"isRetail": false
}
],
"pagination": {
"pageNumber": 1,
"pageSize": 25,
"totalCount": 2,
"totalPages": 1,
"isFirst": true,
"isLast": true,
"totalCountOnPage": 2
}
}
}
Here is my current test code that doesn't work but more or less what I'm working towards.
res=pm.response.json()
let users_len = res.data.users.length
for(let i= 0; i <= users_len; i++){
is_retail = res.data.users[i].isRetail;
if(is_retail == "true"){
pm.globals.set("UserId",res.data.users[i].id);
}else if (is_retail == "false"){
pm.globals.set("orgUserId",res.data.users[i].id);
}
}
I am getting the below error while executing the script.
TypeError: Cannot read properties of undefined (reading 'isRetail')
Can anyone help me with this issue?
答案1
得分: 0
Here are the changes made to the code:
res = pm.response.json();
let users_len = res.data.users.length;
for (let i = 0; i < users_len; i++) {
let is_retail = res.data.users[i].isRetail;
if (is_retail === true) {
pm.globals.set("UserId", res.data.users[i].id);
} else if (is_retail === false) {
pm.globals.set("orgUserId", res.data.users[i].id);
}
}
以上是对代码所做的更改:
- 在for循环中,条件i <= users_len已更改为i < users_len。这确保了循环仅迭代到用户数组的最后一个索引。
- 使用let关键字声明了变量is_retail,以避免任何与作用域相关的问题。
- if语句中的条件现在分别与布尔值true和false进行比较,而不是与字符串"true"和"false"进行比较。这与JSON结构相符,其中isRetail是一个布尔值。
通过进行这些修改,代码现在应正确访问isRetail属性并根据布尔值设置相应的全局变量。
英文:
res = pm.response.json();
let users_len = res.data.users.length;
for (let i = 0; i < users_len; i++) {
let is_retail = res.data.users[i].isRetail;
if (is_retail === true) { // Comparing with boolean true, not the string "true"
pm.globals.set("UserId", res.data.users[i].id);
} else if (is_retail === false) { // Comparing with boolean false, not the string "false"
pm.globals.set("orgUserId", res.data.users[i].id);
}
}
Here are the changes made to the code:
- In the for loop, the condition i <= users_len has been changed to i < users_len. This ensures that the loop iterates only up to the last index of the users array.
- The variable is_retail is declared with the let keyword to avoid any scope-related issues.
- The conditions in the if statements now compare is_retail with the boolean values true and false, respectively, instead of comparing with the strings "true" and "false". This aligns with the JSON structure where isRetail is a boolean value.
By making these modifications, the code should now correctly access the isRetail property and set the corresponding global variables based on the boolean values.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论