Postman - How to get value from json array

Your question may already have an answer on the community forum. Please search for related topics, and then read through the guidelines before creating a new topic.

Here’s an outline with best practices for making your inquiry.

Hi everyone,
I am new to JSON and Postman. I’m trying to do something very simple.

I have a GET request which will get a JSON response like the one below.

In the example below from inside of the data array, I want to get the value of benefitGeneralID but only if the value matches a specific uniqueCode ,eg- name123code

How can I do it? Thanks in advance

Details (like screenshots and the response body):

  • {
    “data”: [
    {
    “setUp”: 5,
    “warrantsLimitation”: true,
    “noOfWarrants”: null,
    “warrantsGrossValue”: null,
    “adminFee”: 3.00,
    “maximumThresholdAmount”: 1000.00,
    “maximumPercentage”: 100.00,
    “impactBudget”: null,
    “impactNetSalary”: null,
    “impactBIK”: null,
    “impactSocSec”: null,
    “benefitGeneralId”: “d928ds4b2d-fsd357--955d-1ef07303c70b",
    “benefitId”: "1a372419-
    -4a16-abd9-75a9da74b9e7”,
    “benefitType”: 22,
    “name”: “Warrant 3”,
    “nameInDutch”: “Warrant 3”,
    “nameInFrench”: “Warrant 3”,
    “startDate”: “2018-10-01T00:00:00.000”,
    “endDate”: “2022-02-03T00:00:00.000”,
    “uniqueCode”: “abcde”
    },

{
“setUp”: 3,
“warrantsLimitation”: null,
“noOfWarrants”: 10,
“warrantsGrossValue”: 100.00,
“adminFee”: 3.00,
“maximumThresholdAmount”: 1000.00,
“maximumPercentage”: 100.00,
“impactBudget”: null,
“impactNetSalary”: null,
“impactBIK”: 100.00,
“impactSocSec”: 0.00,
“benefitGeneralId”: “23bb27c6-9a1e-46f5-8501-567da25fb5e5”,
“benefitId”: “1a372419-43a6-4a16-abd9-75a9da74b9e7”,
“benefitType”: 22,
“name”: “name123”,
“nameInDutch”: “name123”,
“nameInFrench”: “name123”,
“startDate”: “2018-10-01T00:00:00.000”,
“endDate”: “2022-02-05T00:00:00.000”,
“uniqueCode”: “name123code”
},
{
“setUp”: 4,
“warrantsLimitation”: true,
“noOfWarrants”: null,
“warrantsGrossValue”: 1000.00,
“adminFee”: 3.00,
“maximumThresholdAmount”: 10000000.00,
“maximumPercentage”: null,
“impactBudget”: null,
“impactNetSalary”: null,
“impactBIK”: null,
“impactSocSec”: null,
“benefitGeneralId”: “3e3bb656-dbb2--a98c-c36d44b70207",
“benefitId”: "1a372419-
-4a16-****-75a9da74b9e7”,
“benefitType”: 22,
“name”: “Free choice (amount)”,
“nameInDutch”: “name1234code”,
“nameInFrench”: “name1234code”,
“startDate”: “2021-08-30T06:30:00.000”,
“endDate”: “2021-09-30T06:30:00.000”,
“uniqueCode”: name1234code"
},

],
“total”: 2,
“aggregateResults”: null,
“errors”: null
}

1 Like

Hey @mission-saganist-751!

So, to get the benefitGeneralID from the response, we simply loop through the data, test for the required condition and save the required value,

One way to do this is:

//Saving the response from the GET request
var response=pm.response.json();
//Fetching the data array and storing it
var data=response.data, Ids=[];   //Ids to store benefitGeneralID

//looping through each data item
_.each(data, (item)=>{

    //Required condition
    if(item.uniqueCode==="name123code")
       Ids.push(item.benefitGeneralId);
})

console.log(Ids);

Good luck!

1 Like

Thanks for the code :slight_smile:

really appreciate it.

1 Like