Day 09 scripting

Hey y’all. I had a problem with Day 09 Chalange:

Can’t undestang how i can put variable from GET reqquest to the POST request???
If u can - i need a real example how to do this, 'couse with example i can learn how to do this next time.

I’ve already tried: var jsonData = JSON.parse(responseBody) in GET request
pm.environment.set(“email”, jsonData.results.email.name); in POST request
i don’t understand the crux of this point.

Hi @thisishim

Not sure I follow… Can you screenshot what you have tried?


Two things…

“results” is an array (the [ ] brackets in the JSON show this), you will need to specify the index of the array.

Example;

pm.environment.set("email", jsonData.results[0].email.name);

Note: I can’t see the email/name part in your screenshot so I’m assuming that part is not stored as an array also.

Also… the Syntax error you are getting could be because of copy and paste (I’ve had this when copy/pasting from StackOverflow into Postman. The " " marks look like they are in italics. try deleting them and manually typing them back in just to be sure.


Here is the response body. I tried [ ] and “” but its same result

Looking at the layout I would think it is;

let jsonData = pm.response.toJSON();
pm.environment.set("email", jsonData.results[0].email);

This should store “george.defour@example.com” from the above example.

The reason for this is that there is no “name” property in the JSON response. This corresponding property would be “email”

image

By referencing the property we want, the value of that property (aka the email address) then gets stored as the variable.

Hope this helps…

Found my notes from day9… maybe this helps too?

const response = pm.response.json();
console.log(response.results[0].email);
console.log(response.results[0].name.first);
console.log(response.results[0].name.last);
console.log(response.results[0].login.uuid);


ok… what we have)

Can u send to me your day 09 collection? Maybe it will help((((

I’d paste your test sqript but it still doesnt work. Maybe i did it wrong but now i’m really need to overcome difficulties with this

Numbers into [ ] doesnt work

Or maybe u had environment not like mine. Can u screen it?

To save the JSON response as a variable we can do this:

const jsonData = pm.response.json();

To output the “email” property to the console we can do this:

console.log(jsonData.results[0].email);

To save the value of “email” to a variable we can do this:

pm.collectionVariables.set("emailAddress", jsonData.results[0].email);

We have to specify the index of the array (numbers in [ ] brackets) otherwise the code doesn’t know which item in the array we are referring to.

For example, if the response looked like this;

{
    "results": [// <-- Start of Array 
        {//Frist item (index 0)
            "IndexNum": "0"
        }
        {//Second item (index 1)
            "IndexNum": "1"
        }
        {//Third item (index 2)
            "IndexNum": "2"
        }
     ]//<-- End of Array
}

To get the value of the first item we would use;

jsonData.results[0].IndexNum

To get the value of the second item we would use;

jsonData.results[1].IndexNum

To get the value of the third item we would use;

jsonData.results[2].IndexNum

so if we have saved the entire response into the jsonData variable, we can then move through the nested items until we get the value we are after.

Here is a breakdown of each part we are referring to;

Here is my output.

1 Like

I’ll try this tomorrow when i get to pc. I’ll write u my results.
Thanks :slight_smile:

1 Like

@w4dd325 Now i’m learning JS, 'couse i undersood that u need to know JS for autotest)
when i learn it base - i’ll give my feedback. tnx for help)

This post was flagged by the community and is temporarily hidden.