Export json from Firestore

How can we download a json file in the Firebase RTDB console, is there a way to export a json file of Firestore data / document data?

One of my main goals is to compare data before / after updating a document.

+19
source share
6 answers

No, you need to come up with your own process, such as a collection request and a loop around.

Refresh

As of August 7, 2018, we have a managed export system that allows you to upload data to the GCS basket. Although this is not JSON, it is the same format used by Cloud Datastore, so BigQuery understands this. This means that you can import it into BigQuery .

+20
+6

Firestore , , , docs , Firestore.

npm, node-firestore-backup, .

, --accountCredentials path/to/credentials/file.json json , , https://developers.google.com/identity/protocols/application-default-credentials.

  • API.
  • .
  • " ", " ".
  • " " .
  • JSON, "". .
  • *.json, , . ( - ), -.
+3

- Python 2.

https://github.com/RobinManoli/python-firebase-admin-firestore-backup

Firebase Admin Python SDK: https://firebase.google.com/docs/admin/setup

Python:

pip install firebase-admin

Firestore:

pip install google-cloud-core
pip install google-cloud-firestore

( ImportError: Cloud Firestore Python)

Python

# -*- coding: UTF-8 -*-

import firebase_admin
from firebase_admin import credentials, firestore
import json

cred = credentials.Certificate('xxxxx-adminsdk-xxxxx-xxxxxxx.json') # from firebase project settings
default_app = firebase_admin.initialize_app(cred, {
    'databaseURL' : 'https://xxxxx.firebaseio.com'
})

db = firebase_admin.firestore.client()

# add your collections manually
collection_names = ['myFirstCollection', 'mySecondCollection']
collections = dict()
dict4json = dict()
n_documents = 0

for collection in collection_names:
    collections[collection] = db.collection(collection).get()
    dict4json[collection] = {}
    for document in collections[collection]:
        docdict = document.to_dict()
        dict4json[collection][document.id] = docdict
        n_documents += 1

jsonfromdict = json.dumps(dict4json)

path_filename = "/mypath/databases/firestore.json"
print "Downloaded %d collections, %d documents and now writing %d json characters to %s" % ( len(collection_names), n_documents, len(jsonfromdict), path_filename )
with open(path_filename, 'w') as the_file:
    the_file.write(jsonfromdict)
+2
  1. ( firebaseImportExport) npm init
  2. Firebase β†’ β†’
  3. " ", source.json firebaseImportExport.
  4. ( 2 3) destination.json
  5. npm firebase-admin npm.
  6. index.js

const firebase = require('firebase-admin');

var serviceAccountSource = require("./source.json");
var serviceAccountDestination = require("./destination.json");

const sourceAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountSource),
    databaseURL: "https://**********.firebaseio.com" // replace with source
});

const destinationAdmin = firebase.initializeApp({
    credential: firebase.credential.cert(serviceAccountDestination),
    databaseURL: "https://$$$$$.firebaseio.com"
  }, "destination");

const collections = [ "books", "authors",   ...]; // replace with your collections

    var source = sourceAdmin.firestore();
    var destination = destinationAdmin.firestore();
   collections.forEach(colName => {
    source.collection(colName).get().then(function(querySnapshot) {
        querySnapshot.forEach(function(doc) {
            destination.collection(colName).doc(doc.id).set({...doc.data()});
        });
    });
   });

+1

Source: https://habr.com/ru/post/1687066/


All Articles