Return Only 4 Fields From API Call

In a get request that for example returns 250 fields, is there a way to specify returning only 4 of the fields?

Only if the API supports that type of filtering.

So it will be a case of reading the API specification to see if that is possible.

@michaelderekjones - I wasn’t sure if this was possible utilizing the Post Response Scripts section…

You can’t change the response.

You can parse the response to a JavaScript object and use general JavaScript functions like filter and find against that object which the results can then be console logged (or potentially used in the visualizer).

Is it 4 random fields, or 4 specific fields that you want?

Does the response return 250 fields, or 250 records\objects?

JSON responses are an object or array that contains key\value pairs where the values can be another array or object. You can have fairly complicated responses, or fairly flat responses.

If you can provide an example response, then we might be able to help you with the available JavaScript queries.

If the response is an array, please include at least two objects in example, or if it really is a single object with 250 key’s, just include 5-10 keys and provide a bit more context on what you want the the filter to do.

If you post code or JSON, please use the pre-formatted text option in the editor (do not paste images) to prevent everything from being aligned to the left.

The API returns an object literal - sample code is like
“id”: 1234,
“parent_id”: 4441,
“Sku”: “ABC-123”,
“stock_quantity”: 10,
“Status”: “order”

And many many more - i want to parse out
id, sku, stock_quantity and Statuus

Based on the following response.

    "id": 1234,
    "parent_id": 4441,
    "Sku": "ABC-123",
    "stock_quantity": 10,
    "Status": "order"

Taken from here.

javascript - Filter object properties by key in ES6 - Stack Overflow

const response = pm.response.json();

const allowed = ["id", "Sku", "stock_quantity", "Status"];

const filtered = Object.keys(response)
  .filter(key => allowed.includes(key))
  .reduce((obj, key) => {
    obj[key] = response[key];
    return obj;
  }, {});


Which produces the following in the console log.


1 Like

That did it! I didn’t think about logging it to the console, I was thinking the response window would show my filtered results. Thank you!

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