json - 如何将邮递员的回复导出到布局清晰的 csv/xls 文件?
问题描述
我是 API 新手,目前正在从事一个项目来分析来自招聘网站 reed.co.uk 的数据。我正在尝试将数据导出到以变量名称作为标题的电子表格,而不是 json。典型的 GET 请求,例如
curl --location --request GET 'https://www.reed.co.uk/api/1.0/search?keywords=accountant&location=london' \
返回这个:
"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"
等等等等
如果然后我按照 Postman 自己的方法写入 csv ( https://documenter.getpostman.com/view/3407886/RWgp1fB5?version=latest ),则生成的文件仅包含一行包含所有数据 - 如果我使用 xls相反,它们都被放入一个单元格中。理想情况下,每一列都将包含工作变量(如果这是正确的术语),例如 JobId、employerId、employerName 等,并且每一行都是一个新的工作条目。
Reed 的 API 文档在这里:https ://www.reed.co.uk/developers/jobseeker 。
提前致谢。
解决方案
我不想修复现有的 api,所以我写了一个新的。
第 1 步:安装json-2-csv
npm install json-2-csv
第 2 步:将以下代码添加到文件中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");
}
});
});
第 3 步:启动本地应用程序
node .\script.js
第 4 步:在选项卡内的邮递员中提出请求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);
});
第 5 步:检查文件夹Responses
推荐阅读
- python-3.x - 如何在不使用任何 OpenCv 库的情况下旋转具有最近邻和双线性插值的图像?
- javascript - 如何在javascript中调整图像大小
- python - 对象列表上的 __str__ 方法
- python - 将 sklearn 的 BaggingClassifier 与 GridSearchCV 一起使用时出现 ZeroDivisionError
- sparql - 如何在概念搜索中过滤 wikidata 标签?
- sql - 错误代码 1005。无法创建表:“takes”和“Teaches”
- c - 如何通过单击按钮退出 gtk 应用程序?
- python - 我无法将条带包导入到我的 IDE(Pycharm)
- dart - 在 Dart 中为 Map 专业化创建别名
- json - 如何在REACT JS中通过循环分别调用JSON对象的每个键的API请求?