2

I'm trying to take the results of one request in Postman and put them into a second. The first request's format is as follows (leaving out some extraneous objects):

[
   {
      "lfidField": "11111",
      "featureField": "Logging",
      "keyField": "VP316coUxxxxxxx"
   },
   {
      "lfidField": "11111",
      "featureField": "Premium",
      "keyField": "egg0+4Bkmkzsxxxxxxx"
   }
]

There are more objects (as many as 8 in total, but the number can change), but the featureField will always have a unique value. I need to get a simple output of:

{
   "Logging": "VP316coUxxxxxxx",
   "Premium": "egg0+4Bkmkzsxxxxxxx"
}

And obviously that would need to iterate to any number of objects from the first request. I'm extremely new to this, so I'm hoping someone can help.

2
  • you want python code or something? Commented Sep 25, 2021 at 16:02
  • Need this in Chai.js formatted for a Test in Postman, sorry I wasn't clear. Commented Sep 25, 2021 at 19:33

1 Answer 1

1

In tab Test of Request 1:

const res = pm.response.json();
let log = "";
let pre = "";
res.filter((item) => {
    if (item.featureField === 'Logging'){
       log = item.keyField;
    }

    if (item.featureField === 'Premium'){
       pre = item.keyField;
    }
});
pm.environment.set("Logging", log);
pm.environment.set("Premium", pre);

In tab body of Request 2:

{
   "Logging": "{{Logging}}",
   "Premium": "{{Premium}}"
}
Sign up to request clarification or add additional context in comments.

4 Comments

I'm getting the response: "There was an error in evaluating the Pre-request Script:TypeError: Cannot read property 'json' of undefined". I've tried moving a few things around and don't see an obvious problem. Also, can I use "collectionVariables" instead of "environment" to keep my variables contained to the collection rather than the environment? I get the same error either way, just curious.
Oh sorry, my bad. The code should be in Test tab, not Pre-request tab. I’ve updated the answer
That did it! Thank you so very much!!! NOTE: changing "environment" to "collectionVariables" worked just fine.
Yeah, no big differences between environment and collectionVariables. You can choose one of them, and it will work.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.