如何遍历Postman JSON响应并根据条件存储到全局变量中?

huangapple go评论87阅读模式
英文:

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:

{
    &quot;status&quot;: 200,
    &quot;data&quot;: {
        &quot;users&quot;: [
            {
                &quot;id&quot;: 95,                
                &quot;isRetail&quot;: true
            },
            {
                &quot;id&quot;: 118,               
                &quot;isRetail&quot;: false
            }
        ],
        &quot;pagination&quot;: {
            &quot;pageNumber&quot;: 1,
            &quot;pageSize&quot;: 25,
            &quot;totalCount&quot;: 2,
            &quot;totalPages&quot;: 1,
            &quot;isFirst&quot;: true,
            &quot;isLast&quot;: true,
            &quot;totalCountOnPage&quot;: 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 &lt;= users_len; i++){
     is_retail = res.data.users[i].isRetail;
    if(is_retail == &quot;true&quot;){
        
        pm.globals.set(&quot;UserId&quot;,res.data.users[i].id);
    }else if (is_retail == &quot;false&quot;){
        
pm.globals.set(&quot;orgUserId&quot;,res.data.users[i].id);
    }
}

I am getting the below error while executing the script.

TypeError: Cannot read properties of undefined (reading &#39;isRetail&#39;)

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 &lt; users_len; i++) {
  let is_retail = res.data.users[i].isRetail;
  if (is_retail === true) { // Comparing with boolean true, not the string &quot;true&quot;
	pm.globals.set(&quot;UserId&quot;, res.data.users[i].id);
  } else if (is_retail === false) { // Comparing with boolean false, not the string &quot;false&quot;
	pm.globals.set(&quot;orgUserId&quot;, 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.

huangapple
  • 本文由 发表于 2023年5月24日 18:24:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76322495.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定