Reputation: 113
I am working on a flutter project and I need to populate my firestore database with some JSON data.
However, I realized I can upload the data to firebase real time database but cannot transfer it to firestore . I don't want the data on the real time database but on the firestore
**
If anyone knows how to I can populate my firestore database with my JSON data, I'd be really glad.
Thanks in advance
**
Upvotes: 0
Views: 907
Reputation: 314
script.py
file and Copy & Paste the following code in that filefrom firebase_admin
from firebase_admin import firestore, credentials
cred = credentials.Certificate("path/to/serviceAccountKey.json")
firebase_admin.initialize_app(cred)
db = firestore.client()
ref_document = db.collection('new collection').document('new document')
ref_document .set({
"Name": "kojo",
"age": 19
})
firebase-admin
by running following commandpip install firebase-admin
script.py
file by running following commandpython script.py
script.py
file as per your requirementUpvotes: 0
Reputation: 113
Ok. as @Frank Van Puffelen mentioned, there is no direct way to do it. However, I found that if I want to transfer from firestore to real-time database, I will need to first export the real-time database as JSON and then use a programming language e.g. js, python, Go, etc. to update the local JSON on my firestore.
follow these rules
Export the real-time database data to JSON onto your desktop
Return to your firestore project and navigate to the project settings on the right top corner, on the service account tab.
choose the language you wanna use to communicate with your database I used python
select on generate a private key to download it. create a folder and put it there.
copy the code generated for you:
import firebase_admin
from firebase_admin import credentials from firebase_admin import firestore cred = credentials.Certificate("path/to/serviceAccountKey.json") firebase_admin.initialize_app(cred)
pip install firebase-admin
or visit https://pypi.org/project/firebase-admin/#files
db = firestore.client() ref_document = db.collection('new collection').document('new document') ref_document .set({ "Name": "kojo", "age": 19 })
Upvotes: 1
Reputation: 598817
There is no built-in way to transfer data from Cloud Firestore to the Realtime Database, as both databases have a completely different data model.
If you want to transfer the data automatically, you'll have to write a script that:
Upvotes: 1