简体   繁体   中英

how to add a JSON file data into Cloud Firestore + Flutter

i have a huge JSON data with keys and value pairs. like the below code i have so many data in my file

 {
     "Tamil Nadu": {
         "Chennai ": {
            "Anna Nagar": "600040 ",
            "New Avadi road": "600010 "
        },
        "Tiruvallur ": {
         "Ambattur": "600053 ",
           "Mogappair": "600037 "
        
    }

     },
    "Andhra Pradesh": {
        "Anantapur ": {
            "A Narayanapuram": "515001 ",
            "Achampalle": "515621 "
        },
        "Chittoor ": {
             "Agraharam": "517640 ",
             "Badikayalapalle": "517370 "
        }
    }
    
    
}   

how can i add those huge data into cloud firestore database as one of a collection and corressponding documents.

I'm not 100% sure about which Firebase database you are using, since you tagged your question with firebase-realtime-database and google-cloud-firestore and you mentioned "Cloud Firestore" in the title. So let's share some solutions for the two database systems offered by Firebase.

For Firestore

The easiest approach IMHO is to use the Admin SDK from your computer: "The Admin SDK is a set of server libraries that lets you interact with Firebase from privileged environments".

You can create a JS file with, for example, the following content and save it on your computer. After having installed Node.js, you can then run it with the following command: node <filename> .

#!/usr/bin/node
const admin = require('firebase-admin');

admin.initializeApp({
    credential: admin.credential.cert("....json")
});

const data = {
    "Tamil Nadu": {
        "Chennai ": {
            "Anna Nagar": "600040 ",
            "New Avadi road": "600010 "
        },
        "Tiruvallur ": {
            "Ambattur": "600053 ",
            "Mogappair": "600037 "
        }
    },
    "Andhra Pradesh": {
        "Anantapur ": {
            "A Narayanapuram": "515001 ",
            "Achampalle": "515621 "
        },
        "Chittoor ": {
            "Agraharam": "517640 ",
            "Badikayalapalle": "517370 "
        }
    }
}

const promises = [];
const dataArray = Object.entries(data);

dataArray.forEach(d => {
     promises.push(admin.firestore().collection('abc').doc(d[0]).set(d[1]));
})
Promise.all(promises);

You may need to adapt the parsing of the JSON object to cope with the desired Firestore data stucture.


For the authentication, you can initialize the Firebase Admin SDK with the credentials for a service account with the Editor role on your Firebase project with

admin.initializeApp({
    credential: admin.credential.cert("....json")
});

To get the json file shown right above, go to the Firebase console, click on the gear in the top-left corner, select the "Service accounts" tab and click on the blue "Generate new private key" button at the bottom. See also here in the doc.

在此处输入图像描述

For the Realtime Database

Here are three possible approaches:

1/ Use the CLI

The CLI offers several commands to push, update or set data. See also this Firebase blog article .

2/ Use the JSON Import button

In the console you'll found a button as shown below. You can choose under which node you want to upload data but be aware that all existing data at this node will be overwritten"

在此处输入图像描述

3/ Use the Admin SDK

As we did with Firestore you can use the Admin SDK for the Realtime Database. For example:

#!/usr/bin/node
const admin = require('firebase-admin');

admin.initializeApp({
    credential: admin.credential.cert("....json") // See below
});

const data = {
    "Tamil Nadu": {
        "Chennai ": {
            "Anna Nagar": "600040 ",
            "New Avadi road": "600010 "
        },
        "Tiruvallur ": {
            "Ambattur": "600053 ",
            "Mogappair": "600037 "
        }
    },
    "Andhra Pradesh": {
        "Anantapur ": {
            "A Narayanapuram": "515001 ",
            "Achampalle": "515621 "
        },
        "Chittoor ": {
            "Agraharam": "517640 ",
            "Badikayalapalle": "517370 "
        }
    }
}

admin.database().ref('....').set(data);

Note that you may encounter an error from the Database if you try to upload in one shot a very large set of data. You'll then need to upload by chunks.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM