Sid
Sid

Reputation: 613

How to assign GUID to records in Firebase Realtime Database when importing JSON

I need to import a large dataset into a Firebase Realtime Database for a new Android app. Preparing the data is not a problem, I have a VBA script in Excel that does this, however, I would like to have some nodes in the imported database to have a GUID, like the ones generated when "pushing" new data to Firebase. I am not able to find out how to do this.

For example if I import this test dataset:

{
"Genera": {
    "Genus_1": {
        "S001" : {
            "name": "name_1",
            "common_name": "common_1"
        },
        "S002": {
            "name": "name_2",
            "common_name": "common_2"
        }
    }
}

I would like to replace the "Genus_1" and "Sxxxx" strings with a GUID-type string.

If this is possible how would I achieve it. Thanks, Sid

Upvotes: 0

Views: 303

Answers (1)

Frank van Puffelen
Frank van Puffelen

Reputation: 598775

When you import a single JSON snippet into the Firebase Realtime Database, Firebase does not generate any keys for child data for you. This means you have two options:

  1. Import each part in an individual call.
  2. Pre-generate your own keys.

Import each part in an individual call

If you want Firebase to generate a key for specific parts of your JSON, you will need to call the Firebase API for each such part, calling push() or its equivalent for the platform you use. E.g.

curl -X POST -d '{
  "S001" : {
    "name": "name_1",
    "common_name": "common_1"
  }
}' 'https://yourdb.firebaseio.com/Genera.json'

Since you're using POST here, the Firebase server will generate a new key under Genera for the JSON in this call.

Pre-generate your own keys

If you want to import the JSON in one go, you will have to pre-generate the keys yourself. Luckily the algorithm for generating push IDs is available online, and documented in this blog post: The 2^120 Ways to Ensure Unique Identifiers.

Upvotes: 1

Related Questions