How to Import a JSON File to a Firestore collection

Fermin Blanco
2 min readNov 6, 2023
A book library resembling collections

Abstract

Recently I was struggling to add data from a JSON file to a firestore collection. I did find approaches such using a third party service like Firefoo and creating a Full-fledged Firebase application. While I find both cases to solve my needs I endup building a custom, small and single-purpose Firebase function.

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.

Since this operation seems trivial to me, I wonder why the Firebase team have not automate it 🤔.

Firebase products and features

  1. Cloud Storage for Firebase
  2. Cloud Functions for Firebase
  3. Cloud Firestore

Create a Custom Firebase Function

A single-purpose function will suffice our case. The instructions will do the following.

  1. Set a trigger for when a new object(the JSON file) is created in Cloud Storage.
  2. Then download the JSON to memory so our function can play with it.
  3. Parse the file to JSON using the JSON API from node.
  4. Finally, add the json array to a collection in Firestore
Firebase Function for importing JSON files into firebase collection

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 I did find more interesting to be is that the whole blog post was written in one shoot (not mime but the following), it did not require refines whatsoever. And please take a look at the code generation part, it is fairly correct.

Of course the code would not work but it provides a fantastic starting point.

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.

Duet AI wrote an alternative article for me

Resources

--

--