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

1 Answer 1

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()
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.