How to get certain fields?

I am new to postman.

How can I get only the “title”?

Which title?

You have three objects in that response. Do you want all of the titles, the first one, or one related to a specific id?

When posting code including response, can you copy and paste using the Preformatted text option in the editor, as it allows us provide example code without having to jump through several hoops.

If you are new to Postman, then I would suggest a visit to the Learning Centre.

Overview | Postman Learning Center

Including the Postman Training links under “Other Resources”.

image

I would recommend the “Galaxy APIs 101” course first as it gets you used to the application features.

Then the “Galaxy Testing and Automation” course which teaches you how to assert the responses. It includes defining and using variables from responses and should cover your question.

Hi Derick

Assuming that you what to extract the ‘title’ for each of the objects returned in the array, you need to store the contents of the response into a variable. The response is in JSON format, so you could use something like this:

const responseData = pm.response.json();

Now that you have the data stored in a variable called responseData, you need to iterate over the content, extracting only the ‘title’ property values. You could do this as follows:

const recordCount = responseData.length;
let titles = [];
for (let i = 0; i < recordCount; i++) {
    titles[i] = responseData[i].title;
}

You now have all the titles stored in the ‘titles’ variable. you can iterate over the values, something like this:

for (let i = 0; i < recordCount; i++) {
   title = titles[i];
  // the rest of your code here.
}

The above code will simply assign each of the titles, in turn, to the ‘title’ variable. What you want to do with it thereafter is up to you, but hopefully this gives you a base from which you can continue.

Regards
Stephen.

As the response is an array, you can use map functions so you wouldn’t need this loop.

This can be achieved using the JavaScript map function (or the Lodash equivalent).

// parse response
const response = pm.response.json();

// Method 1 - JavaScript core
let titlesJS = response.map(response => response.title);
console.log(titlesJS);

// Method 2 - Lodash module
let titlesLodash = _.map(response, 'title');
console.log(titlesLodash);

I’m not going to comment to much on the second loop unless I know a bit more about the use case and what the original poster wants to test. (But as you are working with an array, you can use a forEach loop).

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.