0

I am using curl

`curl "https://maps.googleapis.com/maps/api/geocode/json?address=$WHERE&key=$API_KEY"`

and the result is

`

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Saket",
               "short_name" : "Saket",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "New Delhi",
               "short_name" : "New Delhi",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "South Delhi",
               "short_name" : "South Delhi",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Delhi",
               "short_name" : "DL",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "India",
               "short_name" : "IN",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "110017",
               "short_name" : "110017",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Saket, New Delhi, Delhi 110017, India",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 28.529262,
                  "lng" : 77.2166529
               },
               "southwest" : {
                  "lat" : 28.517834,
                  "lng" : 77.20113789999999
               }
            },
            "location" : {
               "lat" : 28.5245787,
               "lng" : 77.206615
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 28.529262,
                  "lng" : 77.2166529
               },
               "southwest" : {
                  "lat" : 28.517834,
                  "lng" : 77.20113789999999
               }
            }
         },
         "place_id" : "ChIJ3T8F3fDhDDkRnxNgWBpc2Zc",
         "types" : [ "political", "sublocality", "sublocality_level_1" ]
      }
   ],
   "status" : "OK"
}`

How can I just get the north-east latitudes and longitudes using bash ? Could you please tell me how I should parse it through jq, or another alternative. I need the values to be either saved in different text files. i.e lat.txt and long.txt. I'm planning to pass the latitude and longitude as variables and the use them in another api to get the air quality using the latitude and longitude. Even an alternative would work from which I can scrape the lat and long.

Navan Chauhan
  • 397
  • 6
  • 14
  • 1
    Possible duplicate of [Parsing JSON with Unix tools](https://stackoverflow.com/questions/1955505/parsing-json-with-unix-tools) – Dekker1 Jul 29 '17 at 14:29

2 Answers2

0

This is fairly straight forward we given know that there is only two lines we are concerned with (longitude and latitude) and given that the format of the json is fixed so piping the curl command through awk:

curl "https://maps.googleapis.com/maps/api/geocode/json?address=$WHERE&key=$API_KEY" | awk '/northeast/ {getline;print;getline;print;exit}'

Search for a pattern match northeast and then then read in the next record/line, print (longitude) and then read in the next record and print (latitude)

Raman Sailopal
  • 12,320
  • 2
  • 11
  • 18
0

You can try with jq something like this:

curl ... | jq -r '.results[0].geometry.bounds.northeast | "\(.lat) \(.lng)"'

Output:

28.529262 77.2166529

Or:

curl ... | jq '.results[0].geometry.bounds.northeast | .lat, .lng'

Output:

28.529262
77.2166529
MauricioRobayo
  • 2,207
  • 23
  • 26