Skip to content

Instantly share code, notes, and snippets.

Created June 26, 2011 17:42
Star You must be signed in to star a gist
What would you like to do?
Extract a JSON value from a BASH script
function jsonval {
temp=`echo $json | sed 's/\\\\\//\//g' | sed 's/[{}]//g' | awk -v k="text" '{n=split($0,a,","); for (i=1; i<=n; i++) print a[i]}' | sed 's/\"\:\"/\|/g' | sed 's/[\,]/ /g' | sed 's/\"//g' | grep -w $prop`
echo ${temp##*|}
json=`curl -s -X GET$1.json`
`curl -s -X GET $picurl -o $1.png`
Copy link

grndvl1 commented Sep 22, 2020

Awesome bit of code. Thank you saved me a ton of time and frustrations. Why there is no string compare like a contains just baffles me. The heavy handed use of grep is so silly. All I wanted was a simple if (something.contains(this)); then this allows me to achieve this by parsing the JSON response and then very easily create the if statement. Thank you! You are a time saver!

Copy link

thdoan commented Apr 1, 2021

Given this JSON in data.json:

  "id": "001",
  "color": "blue"

And I want to extract value "blue" from JSON property color in Windows git bash (no jq dependency), here's how I do it in a pinch:

grep color data.json | cut -d'"' -f4

Another way to do it:

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