How to extract data from nested objects in JSON reponse? Then use extracted data in pre-requiste or test script or as variable in other requests

I am able to extract the data from the first object – agency customers for eg reference - 22601 or name - test media services. however unable to extract
data of agencyClients for eg “reference”: “27859” or “name”: “Big Test.”.

Appreciate the help from PM experts and thanks in advance. :slightly_smiling_face:

Request
{
“status”: “Success”,
“timestamp”: “245911807896551”,
“recordCount”: 3,
“agencyCustomers”: [
{
“reference”: “22601”,
“name”: “test Media Services”,
“type”: “Agency”,
“isBookable”: true,
“agencyClients”: [
{
“reference”: “27859”,
“name”: “Big Test.”,
“salesPersons”: [
{
“reference”: “000003243”,
“name”: “Rogers Citizen”
}
]
}
]
},
{
“reference”: “390762”,
“name”: “My Harley-Davidson”,
“salesPersons”: [
{
“reference”: “000003080”,
“name”: “House Syd test”
}
]
},
{
“reference”: “389952”,
“name”: “Inovation test ltd”,
“salesPersons”: [
{
“reference”: “000003080”,
“name”: " test Agency"
}
]
}
]
}

You could use https://jsonpathfinder.com/ that will help show the correct path for the value.

I’ve posted the newly formatted json code below;

{
    
   "status":"Success",
   "timestamp":"245911807896551",
   "recordCount":3,
   "agencyCustomers":[
      {
         "reference":"22601",
         "name":"test Media Services",
         "type":"Agency",
         "isBookable":true,
         "agencyClients":[
            {
               "reference":"27859",
               "name":"Big Test.",
               "salesPersons":[
                  {
                     "reference":"000003243",
                     "name":"Rogers Citizen"
                  }
               ]
            }
         ]
      },
      {
         "reference":"390762",
         "name":"My Harley-Davidson",
         "salesPersons":[
            {
               "reference":"000003080",
               "name":"House Syd test"
            }
         ]
      },
      {
         "reference":"389952",
         "name":"Inovation test ltd",
         "salesPersons":[
            {
               "reference":"000003080",
               "name":" test Agency"
            }
         ]
      }
   ]
}

Using https://jsonpathfinder.com/ you can copy and paste your json code it will give you the data path.

agencyClient Reference:
x.agencyCustomers[0].agencyClients[0].reference

agencyClient Name:
x.agencyCustomers[0].agencyClients[0].name

Hope this helps.

Hi oskarfromaus

it didn’t work getting the below error. Sorry I am new here pls guide me.
Thanks for advising on JSONPATH extracter

var x;
var ref = x.agencyCustomers[0].agencyClients[0].name
postman.setEnvironmentVariable(“ref”, d);

getting this error

There was an error in evaluating the test script: TypeError: Cannot read property ‘agencyCustomers’ of undefined

The x at the start should be replaced with your responseBody. As an example for get request in script:

bodydata = JSON.parse(responseBody)
pm.environment.set(“Ref”, bodydata.agenyCustomers[0].agencyClients[0].name);

That will set the value to a postman variable called “Ref”

Let me know if that works.