【发布时间】:2018-11-30 02:45:24
【问题描述】:
我获取了一个 API 并获得了一个 JSON 对象,我需要对其进行过滤并存储在一个新的本地 json 文件中。
返回结果,但我想不出一种方法来过滤 json 文件(数据)并将其正确写入新的 newfile.json 文件。
.then(
json => {
const data = JSON.stringify(json)
const filtered = data.map( //some filter function )
fs.writeFile('src/data/newfile.json', filtered, 'utf8', function (err) {
if (err) {
return console.log(err);
}
console.log("The file was saved!");
});
这是我从一系列调查中获得的 json 数据示例。
{
"surveyId": 515315,
"reviewId": "34152",
"transactionInfo": {
"transactionRef": "1806001511991",
"transactionCity": "Plymouth",
"transactionState": "MN",
"transactionType": null,
"customerFirstName": "Kristine",
},
"serviceProviderInfo": {
"serviceProviderName": "Lobster",
"serviceProviderEmail": "lobster@bayeq.com",
"serviceProviderId": 43346,
"serviceProviderOfficeName": "Oakdale - 30152501",
"serviceProviderRegionName": "Minnesota - Chacich",
"serviceProviderOfficeId": 721581,
"serviceProviderRegionId": 151563
},
"review": {
"source": "encompass",
"rating": "5.0",
"summary": null,
"description": "He was fantastic ",
"agreedToShare": true,
"verifiedCustomer": true,
"retakeSurvey": false,
"surveyResponses": [
{
"question": "How would you rate the service and support you received?",
"type": "Numeric",
"answer": "5"
},
{
"question": "Please rate the level of advice and communication you received from [name].",
"type": "Numeric",
"answer": "5"
},
{
"question": "How likely are you to refer friends and family to [name]?",
"type": "Numeric",
"answer": "10"
},
{
"question": "How would you rate your overall experience?",
"type": "Experience",
"answer": "Great"
}
],
"reportedAbusive": false
},
"reviewStatus": "completed"
}
如何过滤并创建一个新的 JSON 文件,其结构类似于
{
"email": "foo@bar.com",
"rating": 4.56,
"testimonials": [{
"description": "Fugiat ut sed occaecat.",
"firstname": "Bob",
"city": "Bananatown",
"state": "TH"
}]
}
【问题讨论】:
-
在您粘贴的输入中找不到预期输出中的数据。您能否详细说明一下转换是如何发生的?也许发布更有意义的数据子集和相应的预期输出?
-
什么是用于 dat 获取的 js,如 jqury 或 angular 或其他
标签: javascript reactjs