Need some help with JSON parsing

From Google API I have such JSON returned and I need to extract location:lat and location:lng
(On macOS)

What is the quickest way (least lines of code) to parse out these two location coordinates?

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Kleinlützel",
               "short_name" : "Kleinlützel",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Thierstein District",
               "short_name" : "Thierstein District",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Solothurn",
               "short_name" : "SO",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Switzerland",
               "short_name" : "CH",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "4245",
               "short_name" : "4245",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "4245 Kleinlützel, Switzerland",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 47.4462799,
                  "lng" : 7.4555199
               },
               "southwest" : {
                  "lat" : 47.41085990000001,
                  "lng" : 7.37591
               }
            },
            "location" : {
               "lat" : 47.4254097,
               "lng" : 7.418513299999999
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 47.4462799,
                  "lng" : 7.4555199
               },
               "southwest" : {
                  "lat" : 47.41085990000001,
                  "lng" : 7.37591
               }
            }
         },
         "partial_match" : true,
         "place_id" : "ChIJi4OsKhLCkUcR6iZHghxqLyM",
         "types" : [ "locality", "political" ]
      }
   ],
   "status" : "OK"
}

dim jas as new JSONItem
jas.Load(yourJSONString)

dim lat as string
dim lng as string

lat = jas.Child(“results”).ChildAt(0).Child(“geometry”).child(“location”).value(“lat”)
lng = jas.Child(“results”).ChildAt(0).Child(“geometry”).child(“location”).value(“lng”)

Or…

var results() as variant = ParseJSON( jsonString )
var resultsDict as Dictionary = results( 0 )
var geometry as Dictionary = resultsDict.Value( "geometry" )
var location as Dictionary = geometry.Value( "location" )
var lat as double = location.Value( "lat" )
var lng as double = location.Value( "lng" )

(Untested.)

Great! Thank you Jim and Kem!

I became somewhat rusty with JSON and I’m not seeing the forest for the trees …

I have it working now

1 Like

Great.

Please remember to mark a Solution.

I’m using the code below in Xojo 2020 r2.1.
One still should add some errorhandling and your own Google API key. The one used here is just a (not working) example key.

con = New imUrlConnection

Dim streetNr As String = "790"
Dim street   As String = "Bündtenweg"
Dim city     As String = "Kleinlützel"
Dim zip      As String = "42452"

Dim qry As String = "https://maps.googleapis.com/maps/api/geocode/json?address=" _
+ EncodeURLComponent(streetNr + "+" + street + "," + zip + "+" + city) _
+ "&key=AIzaSyAPoOR_jhakjhIUZhfiuz_dsfT5LaPJcQE"

Var jsonData As String = con.SendSync("GET", qry, 30)
TextArea1.Text = jsonData

Dim jas As New JSONItem
jas.Load(jsonData)

Dim lat,l1 As String
Dim lng,l2 As String

l1 = jas.Child("results").ChildAt(0).Child("geometry").child("location").value("lat")
l2 = jas.Child("results").ChildAt(0).Child("geometry").child("location").value("lng")

lat = Str(l1,"0.#######")
lng = Str(l2,"0.#######")


TextArea2.Text = lat + " / " + lng
1 Like

I find using a JSON editor that displays and formats the string a requirement… looking at unformatted JSON just gives me a big headache. I use “JSON Editor” from Vlad Badea on my Mac… It makes seeing the “path” to the required value real easy.

1 Like

Don’t know if Vlad’s editor is better than my suggestion, but I’m very happy with this Free JSON Editor:

http://www.smartjsoneditor.com

1 Like

Hmm … I must be blind, but I don’t see how I can paste or import a JSON string and then work with it in smart json editor …

+1 on JSON Editor by Vlad Badea. Works great; easy to install via App Store:

1 Like

I just save the JSON string into a plain text file… using BBEdit in my case… and then open that file with the editor.

Easy. just copy the JSON string text with command c etc. into your clipboard, and in SJE you do the following:

Screenshot 2021-06-02 at 10.07.59

1 Like

Ahh! Great - Thanks for the push!

1 Like