I have a response which is a combination of 10 documents and each document will have few unwanted attributes like name, ts, time which should be deleted and the deleted response should be stored as variable in environment file. So, is there any method to fulfill this?
For example, when I am having only one document in response, I am doing in the below manner
let response = pm.response.json()
const responseA= response.Documents[0]
delete responseA[0].name;
delete responseA[0]._ts;
delete responseA[0].time;
But this is not feasible if the document count is huge.
Hey @Harsha,
Welcome to the community
Let’s say the response returns 10s, 100s of documents as an array, then:
pm.response.json().documents.map({name, _ts, time, ...rest} => {
pm.variables.set("key", {name, _ts, time}) // set the deleted fields in a variable
return rest; //this will return the other fields besides the ones you want to delete
})
We need some javascript in the Tests tab to write any custom logic. You could use for-loops as well and other ways, I’m using ES6 syntax in the snippet above, because it’s my personal preference and less verbose but declerative. But you’re free to write a similar thing in an iterative fashion as long as it fulfills your requirement and is easy for your team to work with, I guess
Hey @Harsha ,
Would it be possible for you to share the code you’re working on via a public workspace? I could easily replicate it then and raise a pull request or point the error?