Skip to content

Instantly share code, notes, and snippets.

@khoatran
Created April 3, 2019 07:07
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save khoatran/b60cd53f8dffc592f2240efcba831d59 to your computer and use it in GitHub Desktop.
Save khoatran/b60cd53f8dffc592f2240efcba831d59 to your computer and use it in GitHub Desktop.
Bash parse JSON key
#!/bin/bash
function jsonValue() {
KEY=$1
num=$2
awk -F"[,:}]" '{for(i=1;i<=NF;i++){if($i~/'$KEY'\042/){print $(i+1)}}}' | tr -d '"' | sed -n ${num}p
}
KEY_TO_PARSE=$(curl -s -X GET "JSON_URL" | jsonValue KEY_TO_PARSE 1)
echo $KEY_TO_PARSE
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment