Using jq, HTTPie and the Firebase REST API to persist multiple objects with automatically generated unique ids

Imagine a scenario where you've been developing locally with dummy data and at some point decided to move this data to a Firebase instance.

Assuming your JSON file contents looks like the following.

    "name": "Zapallo Anco",
    "price": 10,
    "unit": "Kg"
    "name": "Zuchini",
    "price": 10,
    "unit": "Kg"

If your design doesn't require the use of unique identifiers for each object, fine, you're good to just go ahead and use this simple bash script.

cat data.json | http --verbose PUT;

However, if you wish to assing a UID to each object inside your array you might want to do something more like the following. This way you're sure to get Firebase to associate your array objects to unique identifiers as key/value pairs inside a reference.

jq -r '.[] | tostring' data.json | while read line;
  do \
    echo ${line} | http --verbose POST; \
    printf '\n\n'; \

If you're app have security policies in place you might need to append a query parameter to the data url your passing to http. You'll need your Firebase app's secret, which you can find under the Secrets panel of your Firebase app's dashboard.


echo '{"key":"value"}' | http --verbose POST auth=="${FIREBASE_SECRET}";

Go ahead and try it. If you're using Homebrew on a Mac you can $ brew install jq httpie to have these binaries available on your command line.

Comment bellow and let me know what you think. Have fun!