Ndheti
Ndheti

Reputation: 276

Edit JSON data to Firebase format

I have json data in one format and need to edit to a format supported by Firebase. I have created a javascript application with function to try to edit the data format but doesnt seem to work. It is a large amount of data but below is a sample.

app.js application

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 file

[
  {
    "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 file

"123456[object Object]444900[object Object]222222[object Object]"

This is the format that is required for 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"
      }
    }
  }
}

Upvotes: 2

Views: 2773

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598765

To convert the data format:

let input = [
  {
    "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"
  }
];

let output = {};
input.forEach((row) => {
  if (!output[row.country]) output[row.country] = {};
  output[row.country][row.tel] = row;
})
console.log(output);

After that you'll need to write it to Firebase or to a file.

Upvotes: 1

Related Questions