I have been working with postman environments for a while now, and have a problem of my own making, which someone may be able to suggest a solution for…
To handle large responses and several tests, I have used environment fields to store JSON.stringify output for later use during the run.
It works pretty well, but creates problems when I then want to export my environments for use as templates for creating csv test data.
I want to do this to work with queries that can then be re-presented as json collection run files, or just submitted as csv collection runners (although that has problems of its own with escaping characters - see other topics).
I need a way to parse through the environment files and safely deliver out the string representations of json so they conform to the csv file columns…
I’m not sure how you’re planning on parsing the environment file, but there’s heaps of CSV node packages which will allow you to safely parse/stringify such that values will work in a CSV column:
If you don’t want to use a whole package, it’s pretty easy to stringify for CSV at a basic level:
const CSV = {
stringify: (str) => {
return `"${str.replace(/"/g, '""')}"`
}
}
CSV.stringify('Some random "string", with a comma and quotes')
-> "Some random ""string"", with a comma and quotes"
Will take a look and have a try, thanks…
… Think I hit a point where I didn’t want to focus on this when I was looking to achieve something else!
My post had a prerequisite that I was exporting environments but thanks for putting the context to the group.
The string parse needed some tweaks but I am now getting csv files through the gates, I still have a problem with key value pairs that contain json and putting that out into a json structure, but I think I should wind that bit back a bit - I am probably over complicating it.
When I have time to properly revisit I will report back
I still have a problem with key value pairs that contain json and putting that out into a json structure
I am not sure where you are using this file after you are getting it “through the gates” but since the JSON responses are in string form you would just need to, in whatever application/language you are using, use a JSON.parse() to get your JSON values back into proper form.
yes.
That wasn’t the problem.
I had json stored within environment fields, as JSON.stringify() strings, and eval-uatable functions.
I overcomplicated things by thinking get the values first then put them into json and csv, instead of extracting json objects straight into json objects, and json keys and values into string arrays for csv output.
I did wind it back, and have a working solution now…