How do I export a response on postman to a clearly laid-out csv/xls file?

Viewed 2352

I am new to APIs and currently working on a project to analyse data from a job website, reed.co.uk. I am trying to export the data to a spreadsheet with the variable names as headers, rather than a json. A typical GET request, e.g.

curl --location --request GET 'https://www.reed.co.uk/api/1.0/search?keywords=accountant&location=london' \

Returns this:

"results": [
    {
        "jobId": 44137966,
        "employerId": 575264,
        "employerName": "REED",
        "employerProfileId": null,
        "employerProfileName": null,
        "jobTitle": "Management Accountant",
        "locationName": "Maidstone",
        "minimumSalary": 28000.00,
        "maximumSalary": 35000.00,
        "currency": "GBP",
        "expirationDate": "28/10/2021",
        "date": "21/09/2021",
        "jobDescription": " My client is looking to recruit an ambitious and commercially aware Management Accountant.  Working closely with the Finance Director, you will provide accurate and timely management accounting information and reports in accordance with strict deadlines.   In addition, you will work closely with the finance team providing support on a daily basis. Please note:  The standard hours of work are 7.00am to 3.45pm, Mo... ",
        "applications": 3,
        "jobUrl": "https://www.reed.co.uk/jobs/management-accountant/44137966"

etc etc

If I then follow Postman's own method for writing to csv (https://documenter.getpostman.com/view/3407886/RWgp1fB5?version=latest) the resulting file only contains one row with all the data - if I do it with xls instead it is all put into a single cell. Ideally each column would contain the job variable (if that is the right term), e.g. JobId, employerId, employerName etc, and each row be a new job entry.

Reed's API documentation is here: https://www.reed.co.uk/developers/jobseeker.

Thanks in advance.

1 Answers

I don't want to fix the existing api, so I write a new one.

Step 1: Install json-2-csv

npm install json-2-csv

Step 2: Add below code to file script.js

const converter = require("json-2-csv");

app.post("/toCsv", (req, res) => {
    let filename = `File_${Date.now()}`,
        filePath = `${path.join(folderPath, filename)}.csv`;

    converter.json2csv(req.body, (err, csv) => {
        fs.writeFileSync(filePath, csv);
        if (err) {
            console.log(err);
            res.send("Error");
        } else {
            res.send("Success");
        }
    });
});

Step 3: Start local app

node .\script.js

Step 4: Make request in postman inside tab Tests.

const list = pm.response.json().results;

const postRequest = {
  url: 'http://localhost:3000/toCsv',
  method: 'POST',
  header: {
    'Content-Type': 'application/json',
  },
  body: {
    mode: 'raw',
    raw: JSON.stringify(list)
  }
};
pm.sendRequest(postRequest, (error, response) => {
  console.log(error ? error : response);
});

enter image description here

Step 5: Check the folder Responses

enter image description here

Related