Test script using main body data

My question:
I want to take the data I have in my body and utilize it in a Test script. Such as, whatever is inputted into the โ€œidโ€ field will be tested against the received data, and I donโ€™t manually have to input it as a parameter. Therefore using โ€œidโ€ in the body as a parameter against the response in a test script.

**Details :
My body layout:
image

My Test layout: (highlighted in yellow is not working)

How I found the problem:
from my understanding, pm.response.json() is the data receieved. Therefore I cannot use it to get the id from the body? I can manually input the id I want to test, which will work.

Side Note:
I tried using jsonData on my Eligible test, but it wasnโ€™t working. Then I used the pm.response.json()[0].Eligible).to.eql(true) and that worked. So I left it.

Iโ€™ve already tried:
I have tried different responses from the help page. I commented out what I have tried but took out the var associated with them from the clutter of hours of testing.
I tried-- โ€œidโ€ : โ€œ{{89784}}โ€ in the body but that caused and Erro 500 process terminating


I welcome any documentation to help me further progress.

I will continue looking at the documentation. If I find a solution, I will take down the post. Thank You.

The following should work.

const requestJson = JSON.parse(pm.request.body.raw);
const responseJson = pm.response.json()

let employeeID = responseJson[0].Employee_ID;

pm.test(`expect employee id to equal ${requestJson.id}`, () => {
    pm.expect(employeeID).to.eql(requestJson.id);
})

1 Like

I appreciate your help. This was the solution. I now know how JSON.parse is used. I read that it was needed to fix the issue, but I didnโ€™t know how it was used. Thank you again.

1 Like