Reputation: 65
I have the following JSON output recieved from a google API call, im strugling with a grep to only store "lat" and "lng" values under "geometry"\"location", so i get an output like:
lat,long
{
"results" : [
{
"address_components" : [
{
"long_name" : "",
"short_name" : "",
"types" : [ "street_number" ]
},
{
"long_name" : "Kildegade",
"short_name" : "Kildegade",
"types" : [ "route" ]
},
{
"long_name" : "Horsens",
"short_name" : "Horsens",
"types" : [ "locality", "political" ]
},
{
"long_name" : "Denmark",
"short_name" : "DK",
"types" : [ "country", "political" ]
},
{
"long_name" : "8700",
"short_name" : "8700",
"types" : [ "postal_code" ]
}
],
"formatted_address" : "Kildegade, 8700 Horsens, Denmark",
"geometry" : {
"location" : {
"lat" : 55.863466,
"lng" : 9.848784
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 55.86481498029149,
"lng" : 9.850132980291503
},
"southwest" : {
"lat" : 55.8621170197085,
"lng" : 9.847435019708499
}
}
},
"place_id" : "ChIJG8X9xAVjTEYRGwL9QuNARJQ",
"types" : [ "street_address" ]
}
],
"status" : "OK"
}
I reckon that i need to make a GREP call after the API KEY, but im kinda stuck, as i get output without any GREP, but nothing when applying any simple GREP.
adresse=$(curl -s "https://maps.googleapis.com/maps/api/geocode/json?address=Kildegade+8700+horsens+denmark"&key=GOOGLE API KEY")
Prior to this i've in some similar JSON output used:
| grep -B 1 "route" | awk -F'"' '/short_name/ {print $4}
But this don't fit into this output, and im just not that powerful in using GREP ;)
Any help would be really helpful, thanks ;)
Upvotes: 0
Views: 566
Reputation: 746
jq (https://stedolan.github.io/jq/) is a lightweight and flexible command-line JSON processor.
Suppose you want to extract lat
and lng
fields from your json sample.
You can then use something like jq '.results[].geometry.location' < input.json
Please note in the example above that I'm considering that your json is stored in an file called input.json
, but you could just use jq
after curl
like this: curl ... | jq ...
Upvotes: 1