How can I update variable values in json body of a request

My request body is a JSON with a list of events. Each event has an eventId and timestamp and it looks like this:
“event”: [
{
“eid”: 1,
“time”: 1538157934251
},
{
“eid”: 2,
“time”: 1538157945251
},




{
“eid”: 50,
“time”: 1538157955251
}]

I want to have ‘eid’ and ‘time’ increment for every event using two variables like…

“event”:
[ {
“eid”: {{id}}+1,
“time”: {{t}}+1000
}, {
“eid”: {{id}}+2,
“time”: {{t}}+5000
},




{
“eid”: {{id}}+n,
“time”: {{t}}+n*1000
}]
I was able to do it with Jmeter. Can I do this with postman?

This is how i did it in Jmeter:
“time”:{__longSum({t},1000, t)}.
For those who are not familiar with Jmeter, the above line adds 1000 to t and stores it back to t

@ravisankarb4u

For time you can use “moment” in your Pre-request scripts.

Example:

//Set Time
var moment = require(“moment”);
var t = pm.variables.get(“t”);
pm.environment.set(‘t’, moment().add(1000, ‘seconds’).valueOf(t));
console.log(“t”, t);

This will pull in your environment variable “t”, add 1000 seconds to the time and then save the it back to the variable “t” to be used in the request.

I have used this in the past with subtracting time to set the timestamp in the past with good success.
Hope this helps.

Mick

Hi Mick,

Thanks for the reply. I want the value to get incremented for each event in that JSON. Not for each request. Do you know of any other solution?

@ravisankarb4u

Ok, I think I see what you’re trying to do.
See if this works.

In your Pre-Request:

//Set Time
var moment = require(“moment”);
var time = moment().valueOf();
pm.environment.set(‘time’, time);

//Set Data
var eventArray = [];
for(var i = 1; i <= 50; i++) {
_ var t = time + (i * 1000);_
_ eventArray.push({_
_ “eid”: i,_
_ “time”: t_
_ });_
}

var data = {“event”: eventArray};
var JSONData = new Buffer(JSON.stringify(data)).toString();
postman.setEnvironmentVariable(“JSONData”, JSONData);
console.log(“JSONDATA”, JSONData);

In your Body:
{{JSONData}}

This is what would be sent:
{“event”:[{“eid”:1,“time”:1538518294839},{“eid”:2,“time”:1538518295839},{“eid”:3,“time”:1538518296839},{“eid”:4,“time”:1538518297839},{“eid”:5,“time”:1538518298839},{“eid”:6,“time”:1538518299839},{“eid”:7,“time”:1538518300839},{“eid”:8,“time”:1538518301839},{“eid”:9,“time”:1538518302839},{“eid”:10,“time”:1538518303839},{“eid”:11,“time”:1538518304839},{“eid”:12,“time”:1538518305839},{“eid”:13,“time”:1538518306839},{“eid”:14,“time”:1538518307839},{“eid”:15,“time”:1538518308839},{“eid”:16,“time”:1538518309839},{“eid”:17,“time”:1538518310839},{“eid”:18,“time”:1538518311839},{“eid”:19,“time”:1538518312839},{“eid”:20,“time”:1538518313839},{“eid”:21,“time”:1538518314839},{“eid”:22,“time”:1538518315839},{“eid”:23,“time”:1538518316839},{“eid”:24,“time”:1538518317839},{“eid”:25,“time”:1538518318839},{“eid”:26,“time”:1538518319839},{“eid”:27,“time”:1538518320839},{“eid”:28,“time”:1538518321839},{“eid”:29,“time”:1538518322839},{“eid”:30,“time”:1538518323839},{“eid”:31,“time”:1538518324839},{“eid”:32,“time”:1538518325839},{“eid”:33,“time”:1538518326839},{“eid”:34,“time”:1538518327839},{“eid”:35,“time”:1538518328839},{“eid”:36,“time”:1538518329839},{“eid”:37,“time”:1538518330839},{“eid”:38,“time”:1538518331839},{“eid”:39,“time”:1538518332839},{“eid”:40,“time”:1538518333839},{“eid”:41,“time”:1538518334839},{“eid”:42,“time”:1538518335839},{“eid”:43,“time”:1538518336839},{“eid”:44,“time”:1538518337839},{“eid”:45,“time”:1538518338839},{“eid”:46,“time”:1538518339839},{“eid”:47,“time”:1538518340839},{“eid”:48,“time”:1538518341839},{“eid”:49,“time”:1538518342839},{“eid”:50,“time”:1538518343839}]}

Mick

Thanks a lot Mick. That solves my problem.