I’m using a CSV file to pass data variables to my requests in the runner. Everything seems to be going great I’m just getting stuck on one variable. I’m trying to pass a list of items but it doesn’t seem to be working.
In my POST request I have…
{“name”: ["{{products"}}]}
and in my CSV file under the products header I have…
{“itemQTY”:2,“itemPrice”:"\u00A32.70",“itemName”:“HOT CHOC”,“itemSKU”:34},{“itemQTY”:1,“itemPrice”:"\u00A32.00",“itemName”:“PAIN AU CHOC”,“itemSKU”:251},{“itemQTY”:1,“itemPrice”:"\u00A32.00",“itemName”:“ENGLISH BREAKFAST”,“itemSKU”:197}
I think I know what the problem is, the product data from the CSV file is being imported as a string instead of the actual format I want it. But I don’t know what I need to do to change that
I’m thinking that you want that product data in JSON format ?
If so, here is a test example where I copied your example string into a variable. I had to change your “ in your post to " in order to make it work.
var productString = '{"itemQTY":2,"itemPrice":"\u00A32.70","itemName":"HOT CHOC","itemSKU":34}';
var productJSON = JSON.stringify(eval('('+productString+')'));
var productJSON = JSON.parse(productJSON);
console.log(productJSON.itemName);
console.log(productJSON.itemPrice);
What I saw in the console …
HOT CHOC
£2.70
Here is what you might do because your “product” variable is being loaded from the CSV file …
var productJSON = JSON.stringify(eval('('+pm.environment.get('product')+')'));
productJSON = JSON.parse(productJSON);
console.log(productJSON.itemName);
console.log(productJSON.itemPrice);