Need compare keys in response with collection variables

Hi everybody!
there is a task to check the keys and values ​​in the response, but keys are in collection variables

request body:

{
“{{textCode}}”: “{{$randomInt}}”
}

where “textCode” is collection variable which we take from another method and it can be random. I need find in responce this key and compare that the request is equal to the response.

response body:

{
“777”: 188,
}

where “textCode” is 777

I try this in Tests postman:

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

let textKey = pm.collectionVariables.get(“textCode”)
console.log(textKey ) //try to check, its OK - “777”

console.log(requestJson.textKey)
console.log(responseJson.textKey) // but here i have - undefined

//and after i want compare with this tests, its didn’t work ofc, because i can’t enter the correct path to the key

pm.test(“Create object. Check values, set variable)”, () => {
if (pm.response.to.have.status(201)) {
pm.expect(requestJson.textKey).to.eql(responseJson.textKey);
pm.collectionVariables.set(“objectText”, responseJson.textKey);
}
});

Thank you Colleagues, help pls:)

The following should work.

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

let textKey = pm.collectionVariables.get("textCode"); // 777

let responseTextKey = responseJson[textKey]
let requestTextKey = requestJson[textKey]

pm.test(`Response Text Key equals ${requestTextKey}`, () => {
    pm.expect(responseTextKey).to.eql(requestTextKey);
})
2 Likes

its work! thank you for help)