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

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

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:

  1. res = pm.response.json()
  2. let users_len = res.data.users.length
  3. for (let i = 0; i < users_len; i++) {
  4. is_retail = res.data.users[i].isRetail;
  5. if (is_retail == "true") {
  6. pm.globals.set("UserId", res.data.users[i].id);
  7. } else if (is_retail == "false") {
  8. pm.globals.set("orgUserId", res.data.users[i].id);
  9. }
  10. }

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:

  1. {
  2. &quot;status&quot;: 200,
  3. &quot;data&quot;: {
  4. &quot;users&quot;: [
  5. {
  6. &quot;id&quot;: 95,
  7. &quot;isRetail&quot;: true
  8. },
  9. {
  10. &quot;id&quot;: 118,
  11. &quot;isRetail&quot;: false
  12. }
  13. ],
  14. &quot;pagination&quot;: {
  15. &quot;pageNumber&quot;: 1,
  16. &quot;pageSize&quot;: 25,
  17. &quot;totalCount&quot;: 2,
  18. &quot;totalPages&quot;: 1,
  19. &quot;isFirst&quot;: true,
  20. &quot;isLast&quot;: true,
  21. &quot;totalCountOnPage&quot;: 2
  22. }
  23. }
  24. }

Here is my current test code that doesn't work but more or less what I'm working towards.

  1. res=pm.response.json()
  2. let users_len = res.data.users.length
  3. for(let i= 0; i &lt;= users_len; i++){
  4. is_retail = res.data.users[i].isRetail;
  5. if(is_retail == &quot;true&quot;){
  6. pm.globals.set(&quot;UserId&quot;,res.data.users[i].id);
  7. }else if (is_retail == &quot;false&quot;){
  8. pm.globals.set(&quot;orgUserId&quot;,res.data.users[i].id);
  9. }
  10. }

I am getting the below error while executing the script.

  1. 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:

  1. res = pm.response.json();
  2. let users_len = res.data.users.length;
  3. for (let i = 0; i < users_len; i++) {
  4. let is_retail = res.data.users[i].isRetail;
  5. if (is_retail === true) {
  6. pm.globals.set("UserId", res.data.users[i].id);
  7. } else if (is_retail === false) {
  8. pm.globals.set("orgUserId", res.data.users[i].id);
  9. }
  10. }

以上是对代码所做的更改:

  • 在for循环中,条件i <= users_len已更改为i < users_len。这确保了循环仅迭代到用户数组的最后一个索引。
  • 使用let关键字声明了变量is_retail,以避免任何与作用域相关的问题。
  • if语句中的条件现在分别与布尔值true和false进行比较,而不是与字符串"true"和"false"进行比较。这与JSON结构相符,其中isRetail是一个布尔值。

通过进行这些修改,代码现在应正确访问isRetail属性并根据布尔值设置相应的全局变量。

英文:
  1. res = pm.response.json();
  2. let users_len = res.data.users.length;
  3. for (let i = 0; i &lt; users_len; i++) {
  4. let is_retail = res.data.users[i].isRetail;
  5. if (is_retail === true) { // Comparing with boolean true, not the string &quot;true&quot;
  6. pm.globals.set(&quot;UserId&quot;, res.data.users[i].id);
  7. } else if (is_retail === false) { // Comparing with boolean false, not the string &quot;false&quot;
  8. pm.globals.set(&quot;orgUserId&quot;, res.data.users[i].id);
  9. }
  10. }

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:

确定