Reputation: 71
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!
Upvotes: 6
Views: 33671
Reputation: 35648
Per the ORIGINAL QUESTION, tagged Realtime-Database, this answer is NOT for Firestore, it's for the older "realtime database":
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 properly 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.
Upvotes: 11
Reputation: 123
For those wondering: there is no option to import it in Firestore (at the moment), 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.
Upvotes: 2