Mark Anderson
Mark Anderson

Reputation: 21

Postman data array to variable in RAW Post Body

I'm using a data file for past man with the following JSON with the following data

{
    "FirstName": "Jennifer",
    "MiddleName": "Geraldine",
    "LastName": "Gemma",
    "email": "[email protected]"
    "Phone":[{"Number":"55-(622)813-5953"},{"Number":"233-(935)372-8021"}]}
}

I'm attempting to do a post with the following data variables in RAW

{
    "FirstName": "{{FirstName}}",
    "MiddleName": "{{MiddleName}}",
    "LastName": "{{LastName}}",
    "email": "{{email}}",
    "Phone": {{Phone}}
}

All the data is being populated with the exception of the "Phone". {{Phone}} is not be substituted.

Is there a way of doing this or an alternative way of posting a JSON request?

OR

Is there an alternative way of posting this information in JSON using Postman from a data file?

Upvotes: 2

Views: 3321

Answers (2)

Rasangani Wewelwala
Rasangani Wewelwala

Reputation: 11

You can convert an array into a string as shown below in the data file.

{
   "FirstName": "Jennifer",
   "MiddleName": "Geraldine",
   "LastName": "Gemma",
   "email": "[email protected]",
   "Phone": "[{\"Number\":\"55-(622)813-5953\"},{\"Number\":\"233-(935)372-8021\"}]}"
}

Upvotes: 1

Unmesh Deshmukh
Unmesh Deshmukh

Reputation: 21

You can use the Pre-req. tab of your request to enter JavaScript code. There you can iterate through the array data.Phone and stringify each entry in the array, JSON.stringify, and construct a concatenated string (say phone_numbers). Then you can set this value to a variable:

postman.setGlobalVariable("phone_nos", phone_numbers). 

This "phone_nos" variable can then be referenced in the RAW request body.

"Phone" : {{phone_nos}}

Upvotes: 2

Related Questions