Can we randomly retrieve data from json file using data driven approach

Hi,

I have a file with 20k records, from this I need to randomly pick around 150 records and then use in my script, is this possible? Can anyone help me with this?

Hey @jency.stella19,

Could you let us know how you’re consuming that data? Is it through the Collection Runner, as a variable in Tests, or through an API?

Hi @arlem thank you for your response.

I’m fetching the data through Collection Runner.

Used modulo operator to fetch the records from the file.

  • In Pre-request I fetched the total records and used in the Test script to do my testing

    //Finding the total number of iterations that are scheduled to run since Total number of iterations === Number of rows in your CSV File

    var list = pm.info.iterationCount;

    console.log(list);

    pm.environment.set(“Total_Records”, list);

  • In Test script I checked the modulo value and did the tests as needed >> IF ($rownumber%(square root of the total number of records in file))=0 {do you test} else {postman.setNextRequest(null);}

2 Likes

Hey @jency.stella19 :wave:

I’m doing some research on a certain workflows flow and was interested in the final code that you have added to the Tests section to get a randomly selected row from the full data file.

Hi @danny-dainton sure :slight_smile:

  • In Pre-request I fetched the total records and used in the Test script to do my testing
pm.environment.set("ROW_NO", data.ROW_NO); // row_no from data file

//Finding the total number of iterations that are scheduled to run since Total number of iterations === Number of rows in your CSV File

var list = pm.info.iterationCount;

console.log(list);

pm.environment.set("Total_Records", list);
  • In Test script I checked the modulo value and did the tests as needed
var Total_Records = pm.environment.get("Total_Records");
var square_value = Math.sqrt(Total_Records);
var round_value = Math.round(square_value);

//console.log(square_value);
//console.log(round_value);

var row_num = pm.environment.get("ROW_NO");

//console.log(row_num);
//console.log((row_num % round_value));

if ((row_num % round_value) == 0) {
    //perform your test
}
else {
    postman.setNextRequest(null);
}

Hope I shared what you expected :slight_smile:

2 Likes

Hi @jency.stella19,

I have a doubt regarding above code.

Suppose we have 100 records in total, round value will come as 10. Modulo operator will return 0 only in case of 10,20,30… Rows.

So basically we are selecting every sqrt(totalrecords)*nth row.

Will we be able to randomise this? If I am not wrong, no matter how many times we run the collection, always same rows will be picked.

Can you please help :slightly_smiling_face: