How to pass a comma separated array of integers in to an API Via CSV Data File

Iā€™m trying to understand how to pass a comma separated array of integers in to an API, via a csv data file.

Iā€™m working with an API which will create users in our application
As part of this API we can assign User Access via Groups within the Application. These are defined with the ā€œgroupsā€ element in the request body.

In a Non-Parameterized request, I pass in:
ā€œgroups": [5008,5009],
The actual ā€œValue Passedā€ in the Request Body, when Viewed in the Postman Console is
groups:
0:5008
1:5009
The request is successfully processed without error and results in the assignment of group IDā€™s 5008 & 5009 being assigned to the user.

For the Parameterized request body for the ā€œGroupsā€ element I have this:
ā€œgroupsā€: ["{{groups}}"],

My Variable is defined as follows:
groups:{{groups}}

In My CSV data file for a single value passed in as ā€œ5008ā€ for the groups variable and the request is successfully processed

The actual ā€œValue Passedā€ in the Request Body, when Viewed in the Postman Console is
groups:
0:ā€œ5008ā€

If I pass in ā€œ5008, 5009ā€ (this should result in the assignment of group IDā€™s 5008 & 5009 being assigned to the user.
The ā€œgroups;ā€ element in the Request Body in the Postman Console is:
groups:
0:"5008,5009"
And I get the following error
Response Body:
type:"JsonReaderException"
reason:"Could not convert string to integer: 5008,5009. Path ā€˜groups[0]ā€™."
details:"Newtonsoft.Json.JsonReaderException: Could not convert string to integer: 5008,5009. Path ā€˜groups[0]ā€™

So Finallyā€¦
How do I need to ā€œFormatā€ the ā€œgroupsā€ value in my csv data file, when it contains a comma separated array of ā€œGroup IDā€™sā€ so that it is properly read by Postman so that it will be properly formatted in the request body that is passed in to my API.

Hi @allen888

There are 2 workarounds:

  1. Youā€™ll need to have a separate delimiter in your CSV file:
    5008|5009|5010
    You can then set a new variable in your pre-request script which replaces the ā€œ|ā€ with ā€œ,ā€: pm.globals.set("comma_sep_groups" , pm.variables.get("groups").replace(/\|/g,",")), and use ā€œgroupsā€: [{{comma_sep_groups}}] in your request body.

  2. Use a JSON data file instead of CSV - youā€™ll be able to store groups in a comma-separated format.

1 Like

The solution provided did not work for us.
We had similar problems with using a JSON Formatted Data file both before and after adding the pre-request script.

No matter what we do with the pre-request script the data winds up as a string
Because it is a string it is not converted to an array of integers which results in the error:
type:"JsonReaderException"
reason:"Could not convert string to integer: 5008,4906. Path ā€˜groups[0]ā€™.ā€œ
details:ā€œNewtonsoft.Json.JsonReaderException: Could not convert string to integer: 5008,4906. Path 'groups[0]'
Working with my developer we added a statement into the Pre-request script suggested, to output the contents of the groups field from the data file to the Console:
var x = pm.variables.get(ā€œgroupsā€).replace(/|/g,ā€,ā€).split(ā€™,ā€™);
pm.globals.set(ā€œcomma_sep_groupsā€, x);
console.log(x);
When I run the request through Runnerā€ It appears in the console as this:
Array:[]
0:"5008"
1:"4906"
Which is what would be expected and is also what we see in the request body when the request is passed in as non-parameterized.

IT appears that when the data is ā€œsavedā€ back into the request body it appears to get flattened back into a string, when the request body is view in the console after the request has been run through ā€œRunnerā€ it appears like this:
groups:
0:ā€œ5008,4906ā€

The Request body of a ā€œGoodā€ request appears like this
groups:
0:ā€œ5008ā€
1:ā€4906ā€

The variable in this case needs to be read as an array not a string

Here, I have a global variable whose value is ā€œ5008,5009ā€.

Variables are always read as strings, but since they are replaced as-is in your request body, [{{groups}}] will resolve to [5008,5009] if groups is a string: 5008, 5009

Removing the .split(ā€™,ā€™) modification (/|/g, not /|/g) will let x be a string. Youā€™ll need to remove the quotes inside the square braces in your request body for it to not be treated as a string.

Hope this helps

1 Like

Thank You!!!

Removing the quotes inside the square braces was the keyā€¦

After removing them the request runs fine.

Didnā€™t actually need the pre-request script either!

Again

Thank You!

Since it looks like some people are still visiting that topic looking for an answer, hereā€™s a collection you can fork that shows how to use an array as a parameter in your request:
https://www.postman.com/postman/workspace/postman-answers/documentation/13455110-dd27f60d-747f-4b3b-896f-6b849506e63c