How to Import a JSON File to a Firestore collection
Abstract
Recently, I was struggling to add data from a JSON file to a firestore collection. And when looking for solutions I found some approaches that would had been good:
- using a third party service such Firefoo or
- creating a Full-fledged Firebase application.
Both cases virtually solve my needs BUT I endup building my custom, small and single-purpose Firebase function to better fit my use case.
STOP THIS MADNESS AND SHOW ME THE CODE
Disclaimer: This article assumes that you have already a project in place with the proper configuration setup. If that happens to not be your case, please refer to the official guides.
This operation seems very trivial to me, and I’m wondering (still) why the Firebase team have not automate it 🤔.
Firebase products and features
Create a Custom Firebase Function
A single-purpose function will suffice our case. The instructions will do the following.
- Set a trigger for when a new object(the JSON file) is created in Cloud Storage.
- Then download the JSON to memory so our function can play with it.
- Parse the file to JSON using the JSON API from node.
- Finally, add the json array to a collection in Firestore
Duet AI for Google Workspace
I did ask Duet AI to help me write a blog post about my use case and it generated an step-by-step guide. What it surprised me the most is that the whole blog post was written in one shoot (results are attached), it did not require refines whatsoever. And please take a look at the code generation part, it is fairly correct.
Of course the code is not perfect and does not compile but it provides a fantastic starting point for anyone to finish it.
Personally I think that the help me write from Duet AI for Google Workspace is a magnificent tool for blueprints and inspirations.
the prompt
help me write a blog post for importing a JSON file from Firebase Storage. Then downloading it into memory using Firebase functions, parse it to JSON, and finally create a collection in Firestore.