Skip to content

Instantly share code, notes, and snippets.

@alanbernstein
Created January 14, 2021 21:51
Show Gist options
  • Save alanbernstein/4725bdd2323fadef33cd78a02f6af2e4 to your computer and use it in GitHub Desktop.
Save alanbernstein/4725bdd2323fadef33cd78a02f6af2e4 to your computer and use it in GitHub Desktop.
curl+jq have a baby
#!/bin/bash
# make an http request via curl
# try to decode it as json with jq
# if it works, pretty print the json
# if it doesn't, ugly print the response
F1="$HOME/tmp.curl"
F2="$HOME/tmp.jq"
curl -s $1 > $F1
cat $F1 | jq . > $F2 2>&1
error=$(head -1 $F2 | grep "parse error")
if [ "$error" == "" ]; then
cat $F2
else
cat $F1
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment