邮递员-如何替换JSON响应中所有特定属性的值,以便稍后将其用作另一个请求的正文 [英] Postman - How to replace values for all specific properties in the JSON Response, to use it later as another Request's Body

查看:119
本文介绍了邮递员-如何替换JSON响应中所有特定属性的值,以便稍后将其用作另一个请求的正文的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有2个API请求。第一个是GET,它返回一个响应。此响应在第二个请求(POST)中用作正文/有效负载。但是有效载荷应该有某些值要在第二个请求中使用之前要替换(在我的情况下,它应该是状态属性的值)。

I have 2 API requests. 1st one is GET, that returns a response. This response is used as a Body/Payload in the 2nd request (POST). BUT the Payload should have certain values to be replaced before used in the 2nd request (in my case below it should be value for "Status" property).

我该如何

这是我的示例响应:

{  
   "Variations":[  
      {  
         "ItemIds":[  
            "xxx"
         ],
         "Items":[  
            {  
               "Id":"67-V1",
               "GuId":"xxx",
               "Type":"Unit",
               "Status":"Active"
            }
         ],
         "Name":"VAR 1",
         "Id":"67-V1"
      },
      {  
         "ItemIds":[  
            "yyy"
         ],
         "Items":[  
            {  
               "Id":"67-V2",
               "GuId":"yyy",
               "Type":"Unit",
               "Status":"Active"
            }
         ],
         "Name":"VAR 2",
         "Id":"67-V2"
      },
      {  
         "ItemIds":[  
            "zzz"
         ],
         "Items":[  
            {  
               "Id":"67-V3",
               "GuId":"zzz",
               "Type":"Unit",
               "Status":"Active"
            }
         ],
         "Name":"VAR 3",
         "Id":"67-V3"
      }
   ],
   "ItemIds":[  

   ],
   "Items":[  

   ],
   "Name":"MAINP",
   "Id":"67",
   "Color":null
}

这是我的代码,但似乎不起作用(替换部分):

Here is my code, but it does not seem to work (the replacement part):

var jsonData = pm.response.json();

function replaceStatus() {
    _.each(jsonData.Variations, (arrayItem) => {
        if(arrayItem.Items.Status !== "NonActive") {

            arrayItem.Items.Status == "NonActive";
            console.log("arrayItem " + arrayItem);
        }
    });
}

pm.test("Run Function", replaceStatus ());

pm.sendRequest({
    url: 'localhost:3000/post',
    method: 'POST',
    headers: {
        "Content-Type": "application/json"
    },
    body: {
        mode: 'raw',
        raw: JSON.stringify(jsonData)
    }
}, (err, res) => {
    console.log(res)
})


推荐答案

我猜您正在尝试将所有 NonActive 值替换为 Active 。在这种情况下,您应该使用 = 进行分配,而不是 == 。您提供的JSON文件无效,因此无法在我的计算机上运行您的代码。我很高兴能仔细查看一下是否奏效

I guess you are trying to replace all NonActive values with Active. In that case, you should use = for assignment not ==. The JSON file you provided is not valid and couldn't run your code on my machine. I am happy to have a closer look if that didn't work

根据您的更新,需要进行这些更改

1-为了处理JSON对象,您需要解析响应,因为它是 string 并且您不能调用sth例如 JsonData.Variations ,确保 jsonData 是JSON对象。

1- in order to deal with JSON object you need to parse the response as it is string and you can't call sth like JsonData.Variations on that.make sure jsonData is a JSON object. if not add sth like this to parse it

var parsedJson = JSON.parse(jsonData)

2-似乎您错过了函数中的一个数组层来迭代。由于您有两个嵌套数组以达到 Status ,因此 replaceStatus 函数应如下所示:

2- It seems that you missed one array layer in your function to iterate over items. As you have two nested arrays to reach Status the replaceStatus function should be as below

function replaceStatus() {
    _.each(parsedJson.Variations, (arrayItem) => {
        _.each(arrayItem.Items, (item) => {
            if(item.Status !== "NonActive") {
                item.Status = "NonActive";
                console.log("arrayItem " + item.Status);
            }
        });
    });
}

这篇关于邮递员-如何替换JSON响应中所有特定属性的值,以便稍后将其用作另一个请求的正文的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆