How to store a request JSON in a variable and use it in the mocked response body

I want to mock exact request values in response.

ex :

request body

“address” : {“city”:“CA”},

response should be like this : based on request i need to map exact values to same key(from request to response - mocking with example)


I have tried adding changes in prescript

var reqBody= pm.request.body.raw
var json_verify = function(reqBody){ try { JSON.parse(reqBody); return true; } catch (e) { return false; }}
    var val = JSON.parse(pm.request.body.raw)
    var bol = true
    for(var key in val)
     if(key === "username"){
       pm.globals.set("userName", key);

my example mocked response body like this :

{"username":{{userName}}} or tried {"username":{{$userName}}} or tried


but when i ran response body is coming as :

{"username":{{userName}}} or {"username":{{$userName}}} or {"username":pm.globals.userName}

expected output:


please suggest me the possible ways. any help much appreciated