Getting error while sending multiple integer by using global variable in request body

{
“name”: “sharan”,
“last”: “pujari”,
“age”: 29,
“sameage”:{{sameageusing}}
}

pre-request script is
var ageval=JSON.parse(pm.request.body.raw).age;

console.log(ageval);

pm.globals.set(“sameageusing”,ageval);

You haven’t included the error message.

However, I’m going to guess that you are getting a JSON error, unexpected token.

This is being caused by the JSON.parse line in your pre-request script.

It can’t parse the JSON in the body because its not valid JSON at this point.

It’s only when it runs, does the variable name gets replaced with the value making it valid JSON.

It will work if you wrap the variable in quotes, but that will make it a string.

"sameage": "{{sameageusing}}"

The following appears to work.

Create your body, but don’t include the ‘sameageusing’ element.

{
    "name": "sharan",
    "last": "pujari",
    "age": 29
}

Then add the sameageusing element in the pre-request script using the following method

const body = JSON.parse(pm.request.body.raw);
body.sameageusing = body.age;
pm.request.body.raw = body;

image