Using and Environment to store stringify'd json then parsing the environment to csv

environments

#1

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…

Any ideas?


#2

Postman API allows for you to get your environments programmatically if that’s an extra step you’re looking to put in:

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:
https://www.npmjs.com/search?q=csv

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"

Does this get you started?


#3

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!


#4

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


#5

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.


#6

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…