Advertisement
metalx1000

Parse JSON in the Linux Shell - BASH

Apr 30th, 2017 (edited)
887
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 2.39 KB | None | 0 0
  1. #get the keys for the output
  2. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r 'keys'
  3. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[]|keys'
  4.  
  5. #get name
  6. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[].name'
  7. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[].name.first'
  8. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[].name.last'
  9. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[]| (.name|.first + " " + .last)'
  10.  
  11. #get Date of birth
  12. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[].dob
  13. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[].dob| "Born on " + .date + " and is " + (.age|tostring) + " years old"'
  14.  
  15. #put it all together
  16. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[]| ("\n"),(.name|.first + " " + .last), (.dob| "Born on " + .date + " and is " + (.age|tostring) + " years old")'
  17.  
  18. #newline char is not needed
  19. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[]| (""),(.name|.first + " " + .last), (.dob| "Born on " + .date + " and is " + (.age|tostring) + " years old")'
  20.  
  21. #let's add a title line
  22. wget "https://randomuser.me/api/?results=20" -qO-|jq  -r '.results[]| ("\n=== User Info ==="),(.name|.first + " " + .last), (.dob| "Born on " + .date + " and is " + (.age|tostring) + " years old")'
  23.  
  24.  
  25. # Open Street Maps Info for Address
  26. address="656 108th Ave N Naples"
  27. url="https://nominatim.openstreetmap.org/search?q=$address&format=json&polygon=1&addressdetails=1"
  28. wget -qO- "$url"|jq '.[]'
  29. wget -qO- "$url"|jq '.[].address'
  30. wget -qO- "$url"|jq '.[].address.postcode'
  31. wget -qO- "$url"|jq '.[].address|"The ZipCode is: " + .postcode'
  32.  
  33. # Search for store
  34. q="publix"
  35. url="https://nominatim.openstreetmap.org/search?q=$q&format=json&polygon=1&addressdetails=1"
  36. wget -qO- "$url"|jq '.[]'
  37. wget -qO- "$url"|jq '.[].display_name'
  38. wget -qO- "$url"|jq '.[].address.county'
  39.  
  40. q="publix naples fl"
  41. url="https://nominatim.openstreetmap.org/search?q=$q&format=json&polygon=1&addressdetails=1"
  42. wget -qO- "$url"|jq '.[]'
  43. wget -qO- "$url"|jq '.[].display_name'
  44. wget -qO- "$url"|jq '.[].address.county'
  45.  
  46. q="publix collier county"
  47. url="https://nominatim.openstreetmap.org/search?q=$q&format=json&polygon=1&addressdetails=1"
  48. wget -qO- "$url"|jq '.[]'
  49. wget -qO- "$url"|jq '.[].display_name'
  50. wget -qO- "$url"|jq '.[].address.county'
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement