Postman Runner posting a json array using a data file

Hi All,

I have a project where I need to use postman runner to “POST” values from a data file to an API endpoint of a service provider.

Below is how my JSON data file is structured

 [{
 	"name": "Example",
 	"custom_fields": {
 		"account_number": "Example",
 		"street": "Example",
 		"city": "Example",
 		"state": "Example",
 		"country": "Example",
 		"zip": "Example",
 		"phone": "Example",
 		"website": "http://www.Example.com"
 	}
 }]

My Post request is set to use the body as raw and is structured like this

{
    "name": "{{name}}",
    "custom_fields": {
        "account_number": "{{custom_fields.account_number}}",
        "street": "{{custom_fields.street}}",
        "city": "{{custom_fields.city}}",
        "state": "{{custom_fields.state}}",
        "country": "{{custom_fields.country}}",
        "zip": "{{custom_fields.zip}}",
        "phone": "{{custom_fields.phone}}",
        "website": "{{custom_fields.website}}"
    }
}

When I run this through runner it pulls the account name {{name}} correctly but anything which is in the JSON array custom_fields just shows up as {{custom_fields.xxxxx}}

Below is what is shown in the body of the runner request

{
    "name": "Example",
    "custom_fields": {
        "account_number": "{{custom_fields.account_number}}",
        "street": "{{custom_fields.street}}",
        "city": "{{custom_fields.city}}",
        "state": "{{custom_fields.state}}",
        "country": "{{custom_fields.country}}",
        "zip": "{{custom_fields.zip}}",
        "phone": "{{custom_fields.phone}}",
        "website": "{{custom_fields.website}}"
    }
} 

Am I using runner incorrectly for what I want to achieve here?

How can I post the fields in the custom_fields array using runner?

Hey @colin.mutch

Great to have you as part of the Postman community :trophy:

As it looks like you’re using all the values and in the same structure from the file, you can just take the whole object.

Adding this in the Pre-request Script:

pm.variables.set(`requestBody`, JSON.stringify(pm.iterationData.toObject()))

Add then adding the variable to the Request Body:

{{requestBody}}

When the Collection Runner does it’s magic, it will grab the data from the file and the script in the Collection will use it in the POST request.

The outcome will be something like this (This is using the Postman Echo service):

2 Likes

Hi Danny,

Thank you for the warm welcome!

And also thank you very much the guidance you gave me has worked wonderfully!

1 Like

You’re the GOAT danny!

1 Like