Home > OS >  JSON Array of Objects Formatting for Postman Test
JSON Array of Objects Formatting for Postman Test

Time:09-28

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.

CodePudding user response:

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}}"
}
  • Related