Created
October 23, 2015 16:07
-
-
Save elrumordelaluz/62844d55a0995752f61a to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Using JSON.sh as a JSON parser | |
# Download https://github.com/dominictarr/JSON.sh | |
function jsonGetValue { | |
./JSON.sh | grep -F -e $1 | cut -f2 | tr -d '"' | |
} | |
# Usage | |
read -r -d '' JSON_DATA <<'EOF' | |
{ | |
"id": 1, | |
"name": "A green door", | |
"price": 12.50, | |
"tags": ["home", "green"], | |
"data": { | |
"image_url": "http://path/to/image" | |
} | |
} | |
EOF | |
# prints JSON | |
echo $JSON_DATA | |
# locate element position with | |
echo $JSON_DATA | ./JSON.sh | |
# output | |
# ["id"] 1 | |
# ["name"] "A green door" | |
# ["price"] 12.50 | |
# ["tags",0] "home" | |
# ["tags",1] "green" | |
# ["tags"] ["home","green"] | |
# ["data","image_url"] "http://path/to/image" | |
# ["data"] {"image_url":"http://path/to/image"} | |
# [] {"id":1,"name":"A green door","price":12.50,"tags":["home","green"],"data":{"image_url":"http://path/to/image"}} | |
# get name | |
echo $JSON_DATA | jsonGetValue '["name"]' | |
# get image url | |
echo $JSON_DATA | jsonGetValue '["data","image_url"]' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment