0

I'm working with postman automation

There is a vehicles array in the body

{
   "Vehicles":[
      {
         "car":"{{car}}",
         "bike":"{{bike}}"
      }
   ]
}

I need to change this like below

{
   "Vehicles":"[{{vehicles}}]"
}

So I have created a pre=request script

let car, bike;
var vehicles = {
    car: data.vehicles.car,
    bike: data.vehicles.bike
}
pm.variables.set("vehicles",vehicles);

I'm getting data from external data file and its as following

[
   {
      "Vehicles":[
         {
            "car":"BMW",
            "bike":"YAMAHA"
         }
      ]
   }
]

I have run it through collection runner. and got the request body as vehicles:[object object] The data is not passing

Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
Erandi Dilshani
  • 335
  • 1
  • 4
  • 11

1 Answers1

4

Here objects are coverted into a string. Due to this the string returned [object,object].Assuming you are using a JSON file for external source you can use JSON.stringify() to solve this. Change the following part of your code:

let car, bike;
var vehicles = {
    car: data.vehicles.car.stringify(),
    bike: data.vehicles.bike.stringify()
}
Christian Baumann
  • 3,188
  • 3
  • 20
  • 37
Shababul Alam
  • 131
  • 1
  • 5