Importing json
Firebase: Import JSON To Cloud Firestore
This can be useful for seeding a firebase project, and or restore a backup.
get users.json
1
{
2
"users": [
3
{
4
"id": "1",
5
"firstName": "Kristin",
6
"lastName": "Smith",
7
"occupation": "Teacher",
8
"reviewCount": "6",
9
"reviewScore": "5",
10
},
11
{
12
"id": "2",
13
"firstName": "Olivia",
14
"lastName": "Parker",
15
"occupation": "Teacher",
16
"reviewCount": "11",
17
"reviewScore": "5"
18
},
19
{
20
"id": "3",
21
"firstName": "Jimmy",
22
"lastName": "Robinson",
23
"occupation": "Teacher",
24
"reviewCount": "9",
25
"reviewScore": "4"
26
},
27
{
28
"id": "4",
29
"firstName": "Zack",
30
"lastName": "Carter",
31
"occupation": "Teacher",
32
"reviewCount": "4",
33
"reviewScore": "5"
34
},
35
{
36
"id": "5",
37
"firstName": "Brad",
38
"lastName": "Rayburn",
39
"occupation": "Teacher",
40
"reviewCount": "2",
41
"reviewScore": "4"
42
}
43
]
44
}
Copied!
[...]
module firestore-export-import
1
// Imports
2
const firestoreService = require('firestore-export-import');
3
const firebaseConfig = require('./config.js');
4
const serviceAccount = require('./serviceAccount.json');
5
​
6
// JSON To Firestore
7
const jsonToFirestore = async () => {
8
try {
9
console.log('Initialzing Firebase');
10
await firestoreService.initializeApp(serviceAccount, firebaseConfig.databaseURL);
11
console.log('Firebase Initialized');
12
​
13
await firestoreService.restore('./data-clean/firebase/users.json');
14
console.log('Upload Success');
15
}
16
catch (error) {
17
console.log(error);
18
}
19
};
20
​
21
jsonToFirestore();
Copied!
Last modified 2yr ago
Copy link