So we get all products, how should we export them in a custom CSV?

Hello everyone in this community, first of all.

We are trying to create a CSV file with products from a warehouse.

We have the Json files, we GET all data, but we did not found a way to create a custom CSV with all these informations , Like product name, title, description, price, currency, image1,2,3 , atributes etc

Would be of help any idea on how to do this.

here is a printscreen of the workspace !

Hey @ogaming,

Welcome to the community :wave:

Just to clarify your question…

Are you trying to create an exportable custom CSV file based on the response body data, from within the application?

Or do you need to create a CSV file with data, to use within the Collection Runner?

I am trying to create a custome CSV file in order to be able and import this file to our site to create products

If you’re trying to gather more information on how to create these files and use these in the Collection runner with your requests - You can get more information about this over on our learning site.

https://learning.getpostman.com/docs/postman/collection_runs/working_with_data_files

So, you can basically make use of this template, which helps you with writing a data from postman to a file.

Documentation for Write Response To File: https://documenter.getpostman.com/view/3407886/RWgp1fB5

All you need to do is basically build the csv manually in the test script and then send it over to the local server which will write it to a file (server is already included as a github repo which you’ll find in the documentation link above and you can use that for your use-case).

To convert your JSON response to a csv format, you’ll need to write the script.
This stackoverflow thread has couple of answers which will help you around that: https://stackoverflow.com/questions/8847766/how-to-convert-json-to-csv-format-and-store-in-a-variable

Next, you need to change the fileExtension that is there for the Write Response To File collection,
Right click the Write Response To File collection > Edit > Tests
You can copy this test script inside and move it to your request’s test script.
Now, change the fileExtension to csv and then write your csv data that instead of the response body.

the thing I don’t get is that the products info and picture links are in one collection and product atributes are into another collection, so we need these in one csv or excel :frowning:

1 Like

Oh, I see.

Well, there are 2 solutions that I can think of right now.

Solution 1: (Easier to do)
You’ll need to modify the node server code (the one that you cloned from GitHub) to actually take the file, read the contents and update each csv entry with the correct picture link.

You can also write your own small server to do that, in case you’re not familiar with javascript and node then you can write your own small file server to actually do that.

Steps would be:

  1. Write all the product data from your PRODUCTS - Get Info as CSV to the file, also write a blank entry for productLink for each item which will be filled in later.
  2. When you send the second request for picture links i.e PRODUCTS - Get Properties, you just need to parse the csv, update each entry with the correct picture link and then save it back to the file.

Solution 2: (Might take a bit longer but would be a good implementation as also all your data will be stored in the cloud)
You can actually make use of the Google Spreadsheets and it’s APIs to create this whole file over the fly.
Google Spreadsheets API docs: Create a spreadsheet  |  Google Sheets  |  Google for Developers
You can send the data to the local server which will call the spreadsheet API and write the data directly to a google spreadsheet file.
Then you can directly export your spreadsheet as a CSV. :slight_smile:

1 Like

Now that Visualizations are out officially, I found a nice and simpler way of creating a CSV output using a handlebar template and got inspired by the description in the docs:

Most of my suggested approach and code is based on the suggested reference formerly shared:

My tests code looks something like:

const items = pm.response.json().products
// maybe filtering some products
// .filter(
//   (product) =>
//     product.price > 100
// )
// choosing desired fields
.map((product) => ({
  name: product.name,
  description: product.description,
  manufacturer: product.manufacturer_name,
  price: product.price,
}))

const csvTemplate = `
name,description,manufacturer,price</br>
{{#each items}}
{{name}},{{description}},{{manufacturer}},{{price}}</br>
{{/each}}
`

pm.visualizer.set(csvTemplate, { items })

With such visualizer in place, after sending the request, in the response’s body tab, one can choose the ‘Visualize’ option of display to see the desired formatted csv file, ready to “Select All → Copy” and paste/use wherever needed :slight_smile: (as well into a Google Spreadsheet).

In general I think the new Visualization feature in general, and the given free-form handlebars templates is a very useful functionality and opens up a whole new world for us

2 Likes

Could the Visualizer be used to visualize the results of a collection runner, especially one or more console.log outputs from hundreds of iterations of the runner?