0

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 ;)

7
  • 4
    Have you considered using a tool that knows how to parse json instead of grep, like jq? Commented Nov 8, 2017 at 13:27
  • Im not familiar with jq - can it be used together with curl ? im storing my output in a file for each line, and have a larger size of variables to run through... Commented Nov 8, 2017 at 13:36
  • 1
    I don't know what you're asking there. You can do curl ... | jq ... if that's what you are asking Commented Nov 8, 2017 at 13:45
  • Ok thanks i will look into jq, thanks for pointing me in a direction none the less ;) Commented Nov 8, 2017 at 13:46
  • 1
    I don't understand what you're trying to accomplish here, but suppose you want to extract lat and lng from that json: you can ust use jq ( stedolan.github.io/jq ) like the following: jq '.results[].geometry.location' < input.json Please note in the example above that I'm considering that the json is stored in an file called input.json Commented Nov 8, 2017 at 13:47

1 Answer 1

1

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 ...

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.