2

So I have what's probably a super easy question but can't get my head around it. In the post request body I have a large json file with the format:

{
"order": {
"id":{{orderId}},
"numberofboxes":{{nbrboxes}},
"partnbrs [
{ 
"partId": {{partnumber}},
"description":{{descrip}},
"price": {{price}}
}]
},
"customer1":"{{customername}}
} 
 

My csv has the follwing format:

orderId,nbrboxes,partnumber,descrip,price,customername
AB1,3,PF1234,battery,50,johnDoe
AB1,3,PT2345,battery,40,johnDoe
AB1,3,PT2345,cable,40,johnDoe

Any ideas how I can read several rows from 1 csv into 1 post request? If I have only one row the runner in postman works just fine. The issue is that it reads the seperate rows as different iterations/post requests when they are actually the same. Thanks

Danny_ds
  • 11,201
  • 1
  • 24
  • 46

1 Answers1

1

Reading CSV file in Postman

CSV:
a,b,c
1,2,3
1,5,6
2,4,6

CSV parse output:

Approach 1: store CSV content as an environmental variable

Just copy the CSV file content and create an environmental variable and paste the content inside it

Then in test or pre-requisite step:

console.log(pm.environment.get("data"))
const parse = require('csv-parse/lib/sync')
//Environmental variable where we copy-pasted the csv content
const input =pm.environment.get("data");
const records = parse(input, {
 columns: true,
 skip_empty_lines: true
})

console.log(records)

Approach 2: Passing the CSV content to an environmental variable through Newman

(Powershell): Then in test or pre-requisite step use the same code as above:

> $a= Get-Content -raw .\1.csv
> newman run .\test.postman_collection.json -e .\test.postman_environment.json -k -r "cli,htmlextra" --env-var data=$a

Csv parse output:

enter image description here

it will be converted as an array of objects , you can call specific row and value as

records[3]['a'] 

THis gives 4th row andcolumn 'a'

gif:

enter image description here

PDHide
  • 18,113
  • 2
  • 31
  • 46
  • Thanks @pdhide, could you explain how I would copy-paste the csv values please? What would the variable look like? I think that is the part I am stuck on. The main issue is that my json schema needs to have a fixed structure but the number of objects and arrays can vary. My csv file adjusts to that structure but I can have several rows or just one. What I don't know how is how to create more lines in my body request depending on the number of objects/rows I have in my csv. I could have 1 part or 2 or 3 and the part number can even be the same, just different price or description. – Mariamstack Feb 01 '21 at 11:09
  • @Mariamstack just open your csv in notepad and copy all the content as it is and paste it as it is to an variable – PDHide Feb 01 '21 at 11:12
  • I'm afraid it's not working. What I don't understand is how the environment variable connects to my schema. Or even how the csv is connected to the environment. Could you please explain so I can try and fix/adjust somewhere please? I was using runner btw. – Mariamstack Feb 01 '21 at 11:59
  • so my json can look like this {"order": { "id": "AB1", "numberofboxes":"3", "partnbrs [ { "partId": "PF1234", "description":"battery", "price": "50" }]}, "customer1":"johnDoe" } – Mariamstack Feb 01 '21 at 12:10
  • @Mariamstack added the gif please see the video , you have to extract the value and again set that value as variable `pm.environment.set("customername",customername)` – PDHide Feb 01 '21 at 12:18
  • make sure to add double quotes for string – PDHide Feb 01 '21 at 12:19
  • thanks for the gif. Not sure this is what I'm looking for. My body schema has 100 lines vs csv with only 28. The other values in schema are hardcoded as they won't vary. What I want is the csv to add as many objects (partnbrs with their properties) as there are in the csv into one post request and part of the same order/schema so like partnbrs[ {"partId":"1"},{ {"partId":"1"},{"partId":"2"}]. Another question, the values in the csv will be different in each run. I assume the copy paste is only done once and the values will there on be taken from the file loaded in runner? – Mariamstack Feb 02 '21 at 10:02
  • assume your json is `{ name:"something" , "age":40}`, you can leave what ever you wnat as hardcoded and make others as dynamic by using `{ "name" :"something" , age : {{age}} }` – PDHide Feb 02 '21 at 10:07
  • to get age from first row records[1]['age'] and set it as environment `pm.environment.set("age",records[1]['age'])` in prerequest – PDHide Feb 02 '21 at 10:09
  • Thats the only way to use CSV , postman cannot access external file other than the data file – PDHide Feb 02 '21 at 10:10
  • thanks for your help. That is similar to what I did initially. what I can't get it to do is to dinamically add all the values, so I can have 2 names or 3 or 4 and I want it do add as many as in the file. In your example the result it would be {"a": 1, "b":2, "c":3}, {"a": 1, "b":5, "c":6} but the next could be just {"a": 1, "b":2, "c":3} with no extra rows – Mariamstack Feb 02 '21 at 10:34