Get properties from JSON object by name

I wrote this script to check equality of custom attributes of object

pm.test("…", function () {
var jsonData = pm.response.json(),
propertiesToCheck = [‘availableToClient’, ‘contextId’, ‘defaultValue’, ‘description’,
‘label’, ‘sectionId’, ‘valueType’, ‘values’],
body = JSON.parse(pm.environment.get(“body”));

propertiesToCheck.forEach((p) => 
    pm.expect(jsonData), body.p));


Variable body contains body of my request, f. e.

var body = JSON.stringify(
“availableToClient”: true,
“contextId”: 1,
“defaultValue”: 2,
“description”: “some desc”,
“label”: “some label”,
“sectionId”: 1,
“valueType”: “INTEGER”,
“values”: [1, 2, 3]

pm.environment.set(‘body’, body);

But it doesn’t work that way, because expression body.p means get attribute with the name “p”. If there is a way to read attribute with the name which is written at variable?

Instead of writing body.p, you have to write body[p]

1 Like

Thanks, just got it at the moment!

1 Like