Skip to content

Instantly share code, notes, and snippets.

@elrumordelaluz
Created October 23, 2015 16:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save elrumordelaluz/62844d55a0995752f61a to your computer and use it in GitHub Desktop.
Save elrumordelaluz/62844d55a0995752f61a to your computer and use it in GitHub Desktop.
# 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