Last Updated: May 06, 2016
·
399
· mdeiters

Parsing JSON from the command line to download random avatars

I needed real avatars for a prototype app. UIFaces has a collection of avatars people have authorized for this use. They even have a simple API which gives you a random avatar. I downloaded a few with this simple little script:

curl http://uifaces.com/api/v1/random | jsawk 'parts = this.image_urls.normal.split("/"); file = parts[parts.length - 2] + "." + parts[parts.length - 1]; out(this.image_urls.normal); return file' | xargs -n 2 sh -c 'wget -O $2 $1' argv0

Note: You need jsawk to run this. I'm on a mac so it was easy to install with homebrew: brew install jsawk

Run this command if you want to download 100 avatars:

for i in {1..100}; do curl http://uifaces.com/api/v1/random | jsawk 'parts = this.image_urls.normal.split("/"); file = parts[parts.length - 2] + "." + parts[parts.length - 1]; out(this.image_urls.normal); return file' | xargs -n 2 sh -c 'wget -O $2 $1' argv0; done