Save a value from response as a environment variable

Hi

I am trying to grab my result and ad as a variable to my environment.

This is what i’m trying

let firstResponse = pm.response.json();

let productId = firstResponse.productId;
pm.environment.set('productId', productId);

and im trying to use that to grab productId. well to be honest I am also going to need the “apiProviderId” and “name”


{
    "totalRecords": "4368",
    "fetchedRecords": 1,
    "totalDisplayRecords": "1",
    "results": [
        {
            "productId": "41669",
            "apiProviderId": 0,
            "name": "Morocco Uncovered",
            "productClass": "Y",
            "rating": null,
            "ticketEntryFieldName": null,
            "ticketEntryFieldFormat": null,
            "ticketEntryFieldDescription": null,
            "isOverlayed": false,
            "productOverlayId": null
                }
            ],
            "agentCategories": []
        }
    ]
}

Can anyone help and suggest what I should enter under test to grab all those varibles?

Sorry, I know, biggest noob =o)

@KennyAdventium Welcome to the Community :partying_face:

I was a newbie too when I started, we all had to start someplace right :blush:

So I understand that you are new to this JSON response parsing. So my few suggestions here:

  1. Console.log() → Try to print something if you have doubts. You can start and proceed element by element to trace the path
  2. https://jsonpathfinder.com/ → Pasting your response will give the path. I see some error in your response, seems like you missed some part of it.

Example:

let firstResponse = pm.response.json();

let productId = firstResponse.results[0].productId;
let apiProviderId= firstResponse.results[0].apiProviderId;
let name= firstResponse.results[0].name;

I am just guessing this :grinning:

Incase is results is an array with more than one element then you need to loop to get every productId,apiProviderId and name in the response.

I hope this helps :blush:

@bpricilla thank you so much, for sure need to start somewhere. thanks for the understanding.

I have updated and it works, big thanks.

You are right, I copied just part of the results as 704 rows and didnt think I would need to add all.

Can I ask another question?

So now I am using this to set the variable to the environment, do you know if there is a cheat way so I dont have to type out all my 100s of variables?

let firstResponse = pm.response.json();

let productId = firstResponse.productId;
let apiProviderId = firstResponse.results[0].apiProviderId;
let name = firstResponse.results[0].name;

pm.environment.set('productId', productId);
pm.environment.set('apiProviderId', apiProviderId);
pm.environment.set('name', name);