kojo justine
kojo justine

Reputation: 113

Populate firestore database with json data from firebase real time database

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

I want move this firestore enter image description here

into this firestore database enter image description here

**

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

Answers (3)

Israr
Israr

Reputation: 314

Step-1 :

Create a script.py file and Copy & Paste the following code in that file

from 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
})

Step-2 : Install firebase-admin by running following command

pip install firebase-admin

Step-3 : Download the service-account json file from Firebase

Step-4 : Run the script.py file by running following command

python script.py

Step-5 : Modify the script.py file as per your requirement

Upvotes: 0

kojo justine
kojo justine

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

  1. Export the real-time database data to JSON onto your desktop

  2. Return to your firestore project and navigate to the project settings on the right top corner, on the service account tab.

  3. choose the language you wanna use to communicate with your database I used python

  4. select on generate a private key to download it. create a folder and put it there.

  5. 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)
  1. open your python IDE and paste the code.
  2. save your python script in the same folder as the private key file
  3. You cannot just use it yet, you need to install the python firebase admin library

pip install firebase-admin

or visit https://pypi.org/project/firebase-admin/#files

  1. the installation is successful, then you will have access to your database from python. add the ff. code
db = firestore.client()
ref_document = db.collection('new collection').document('new document')
ref_document .set({
"Name": "kojo",
"age": 19
})
  1. Now run your code and refresh your firestore database

Upvotes: 1

Frank van Puffelen
Frank van Puffelen

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:

  1. Reads the data from Realtime Database through its API.
  2. Converts the JSON structure to a bunch of documents.
  3. Writes the data to Firestore through its API.

Upvotes: 1

Related Questions