Skip to content

Instantly share code, notes, and snippets.

@n370
Last active June 21, 2020 19:57
Show Gist options
  • Star 4 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save n370/df3331a0deae3e5445ed to your computer and use it in GitHub Desktop.
Save n370/df3331a0deae3e5445ed to your computer and use it in GitHub Desktop.
Using jq, HTTPie and the Firebase REST API to push JSON objects from your terminal.

Using jq, HTTPie and the Firebase REST API to push JSON objects from your terminal.

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 https://my-firebase.firebaseio.com/test.json;

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 https://my-firebase.firebaseio.com/test.json; \
    printf '\n\n'; \
  done;

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.

FIREBASE_SECRET="0f5some3b550a0a137dsecret478012a3" 

echo '{"key":"value"}' | http --verbose POST https://my-firebase.firebaseio.com/test.json 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.

References

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment