[英]BASH script - return two specific values from JSON output using GREP
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: 我从Google API调用中收到以下JSON输出,我在用grep挣扎着只在“ geometry” \\“ location”下存储“ lat”和“ lng”值,因此得到如下输出:
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. 我认为我需要在API KEY之后进行GREP调用,但是我有点卡住了,因为我得到的输出没有任何GREP,但是在应用任何简单的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: 在此之前,我曾使用过一些类似的JSON输出:
| 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 ;) 但这并不适合此输出,并且在使用GREP方面还不够强大;)
Any help would be really helpful, thanks ;) 任何帮助都会非常有帮助,谢谢;)
jq ( https://stedolan.github.io/jq/ ) is a lightweight and flexible command-line JSON processor. jq( https://stedolan.github.io/jq/ )是轻量级且灵活的命令行JSON处理器。
Suppose you want to extract lat
and lng
fields from your json sample. 假设您要从json示例中提取
lat
和lng
字段。
You can then use something like jq '.results[].geometry.location' < input.json
然后,您可以使用
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 ...
请注意,在上面的示例中,我正在考虑将您的json存储在一个名为
input.json
的文件中,但是您可以在curl
之后使用jq
,例如: curl ... | jq ...
curl ... | jq ...
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.