parse json by jq
install on mac
- mac :
brew install jq
install on linux
- linux(32bit) :
wget http://stedolan.github.io/jq/download/linux32/jq
- linux(64bit) :
wget http://stedolan.github.io/jq/download/linux64/jq
chmod +x ./jq
sudo cp jq /usr/bin
example file
1 |
|
use
- To parse a JSON object:
cat json.txt | jq '.name'
- output : “Google”
- To parse a nested JSON object:
cat json.txt | jq '.location.city'
- output : “Mountain View”
- To parse a JSON array:
cat json.txt | jq '.employees[0].name'
- output : “Michael”
- To extract specific fields from a JSON object:
cat json.txt | jq '.location | {street, city}'
- output :
1
2
3
4{ "city": "Mountain View", "street": "1600 Amphitheatre Parkway" }
- output :