我有一种格式的json数据,需要编辑为Firebase支持的格式。我已经创建了一个javascript应用程序,其功能是尝试编辑数据格式,但似乎无法正常工作。这是一个大量的数据,但下面是一个样本。
app.js申请
var fs = require('fs');
//read data from file
var readData = fs.readFileSync('readMe.json','utf8');
var sample = JSON.parse(readData);
var newData =addFirebaseKey(sample);
// output datda to output.json
fs.writeFileSync('output.json',JSON.stringify(newData),'utf8');
function addFirebaseKey(contacts){
var fireContacts="";
for(var i=0;i<contacts.length;i++){
var tempObj=contacts[i];
//tel number is also the firebase key
var fireKey =tempObj.tel;
fireContacts=fireContacts+fireKey+tempObj;
}
return fireContacts;
}
readMe.json文件
[
{
"contactName": "Office of the President",
"description": "office of president",
"fax": "-",
"location": "Lusaka",
"postalAddress": "-",
"tel": "123456",
"country": "Zambia"
},
{
"contactName": "State House",
"description": "State president, Ministry of",
"fax": "-",
"location": "Lusaka",
"postalAddress": "-",
"tel": "444900",
"country": "Zambia"
},
{
"contactName": "National Strategy Office",
"description": "national strategy",
"fax": "-",
"location": "Gaborone",
"postalAddress": "-",
"tel": "222222",
"country": "Zambia"
}
]
output.json文件
"123456[object Object]444900[object Object]222222[object Object]"
这是Firebase
所需的格式{
"contactDetail" : {
"Zambia" : {
"123456" : {
"contactName" : "Shocks & Exhaust Fitment Centre",
"description" : "motor vehicle exhaust systems",
"fax" : "-",
"location" : "Lusaka",
"postalAddress" : "NA",
"tel" : "123456"
},
"888555" : {
"contactName" : "K Media",
"description" : "internet marketing",
"fax" : "-",
"location" : "Lusaka",
"postalAddress" : "P O Box 26249, Gaborone",
"tel" : "888555"
},
"555544" : {
"contactName" : "Shocks & Exhaust Fitment Centre",
"description" : "secretarial services",
"fax" : "-",
"location" : "Lusaka",
"postalAddress" : "NA",
"tel" : "555544"
}
}
}
}
答案 0 :(得分:1)
转换数据格式:
FakeCity
&#13;
之后,您需要将其写入Firebase或文件。