Request from another response for updating the values

I have created this data by using Post and I have stored total response in one variable.

Now I want to update the title value as “welcome” ,using put.

{
“mid”:“574800”,
“pay”:"{\“id\”:\“d4683215-b2c1-4c0d-bd8b-e7e42f28a3a\”,\“templateId\”:\“d9297e66-29cb-4425-8555-332a71a5a30a\”,\“title\”:\“welcome}”,
“svc”:“task”
}

Test script:

var response=JSON.parse(responseBody);
pm.globals.set(“variable”,JSON.stringify(response);

Pre-request script:

var request=JSON.parse(pm.globals.get("variable));
request.pay.title=“welcome”;
console.log(request);
pm.globals.set(“req_body”,JSON.stringify(request));

In body:

{{req_body}}

I have tried in this way but the value was not updating. Can you please help me on this.

And in console window “request” variable the data was storing in this format after using the parse.

{
“mid”:“574800”,
“pay”:"{“id”:“d4683215-b2c1-4c0d-bd8b-e7e42f28a3a”,“templateId”:“d9297e66-29cb-4425-8555-332a71a5a30a”,“title”:“welcome}”,
“svc”:“task”
}

@michaelderekjones @w4dd325 @sseenivasan89 @pooja-mistry @danny-dainton

please can help me on this.

First things first. JSON.parse on the response body is the old way of achieving this.

Use the Postman library instead.

const response = pm.response.json();

When you set the global variable. You are stringifying it first, which is ok, but it means you must parse it when retrieving. This bit is done using the JSON.parse method.

Why a global variable. You sure you want this available to all Postman collections? I would recommend a Collection or Environment variable.

However, your pay element is also wrapped in a string, which is probably why you are having issues. It’s not an object at this point in time, so you can’t target it using.

request.pay.title=“welcome”;

It’s a single string with an object in it.

JSON is key value pairs, and the values can be strings, objects, array objects, etc. It can get complex.

In this case its a string with an object in. (If you are in control of this API, you might want to have a word with the developer, because why on earth is it like this instead of a straight up object, which would make the targeting of elements easier).

You will need to parse that again. Separately. Before you can target the elements within it.

Parse it to a variable. This will allow you to target the elements, and you will need to stringify it when you save it back to the pay element within the request variable.

Finally, please get used to using the preformatted text option in the editor when pasting code or JSON. It’s stops everything getting aligned to the left, which makes it really hard to read and spot issues.