简体   繁体   中英

How to upload JSON file to firebase

I've a large JSON file, containing a lot of data. I want to upload it to firebase, but obviously can't enter everything one by one. Can someone please tell me exactly how can I do this? Thanks!

There's a super easy to use IMPORT button in the firebase console. Open your browser, go to your Firebase console->Three Dots on the right->Import JSON.

Important to note that when importing, whichever node you have selected in your Firebase database will be overwritten, so make sure you don't have your root node selected - create a child node and then do your import.

It does need to be a property formatted JSON file - there are a number of online sites that will check the validity of the file. One file we were trying to import had a missing key - after not finding it while scanning we copy and pasted it into one of the sites and quickly found the issue - so the short story there is your nodes needs to have unique keys to be imported.

在此处输入图片说明

For those wondering: there is no option to import it in Firestore, you have to do it programmatically.

Here is what I came up with after a lot of search (in my React project).

I added this code next to the Firestore database setup. Basically you just loop through your data and add each item 1 by 1.

Beware: it seems that there is an (unknown to me) limited number of additions possible at once so I had to split the thing by adding some condition every 100 items.

...

const db = admin.firestore();

myStuff.forEach((obj, index) => {
  if(index <= 100) { // manually change this condition to bypass limitations
    db.collection("myCollection")
      .add({
        id: obj.id,
        name: obj.name,
      })
      .then(function (docRef) {
        console.log("Document written with ID: ", docRef.id);
      })
      .catch(function (error) {
        console.error("Error adding document: ", error);
      });
  }
});

Hope it helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM