I need to modify json file and not sure how to do it.
First, I send GET request that returns JSON:
{
"id": 100,
"info": {
"type": "abc",
"product": null
}
}
…and I save it into var:
var responseData = pm.response.json();
pm.environment.set("body", responseData);
Next, I need to send PUT request with updated json file. If product=null it will look like :
{
"id": 100,
"info":
"type": "abc",
"product": {
"productId": 123,
"info": {
"available": true,
"restock": {
"enabled": true
}
}
}
}
In my pre-request script I have the following:
var requestBody = pm.environment.get("body");
if (requestBody.info.product !==null) {
requestBody.info.product =null
} else {
jsonObject = JSON.parse(requestBody);
jsonObject.product.push({"productId":"123"});
// I am not sure how to add the rest...
pm.environment.set("updatedBody", JSON.stringify(requestBody));
When I run it I am getting the error: JSONError: Unexpected token ‘o’ at 1:2 [object Object] ^