JSON response file provided by Google Geocode is having duplicate column types, and showing error "Msg 13607, Level 16, State 4, Line 163"

28 views Asked by At

The response file from Google - Sample one.

From this response I am trying to fetch latitude and longitude from json response. But it is throwing this error:

Msg 13607, Level 16, State 4, Line 163
JSON path is not properly formatted. Unexpected character 'r' is found at position 1.

I think it is because of the types column duplicate in the .json file response. Can we ask google to provide as type1 and type2.

As I will be doing automation logic over this piece of code. Your suggestions and guidance will be highly appreciated.

Here's the JSON and code:

{
    "results" : [
       {
          "address_components" : [
             {
                "long_name" : "1600",
                "short_name" : "1600",
                "types" : [ "street_number" ]
             },
             {
                "long_name" : "Amphitheatre Parkway",
                "short_name" : "Amphitheatre Pkwy",
                "types" : [ "route" ]
             },
             {
                "long_name" : "Mountain View",
                "short_name" : "Mountain View",
                "types" : [ "locality", "political" ]
             },
             {
                "long_name" : "Santa Clara County",
                "short_name" : "Santa Clara County",
                "types" : [ "administrative_area_level_2", "political" ]
             },
             {
                "long_name" : "California",
                "short_name" : "CA",
                "types" : [ "administrative_area_level_1", "political" ]
             },
             {
                "long_name" : "United States",
                "short_name" : "US",
                "types" : [ "country", "political" ]
             },
             {
                "long_name" : "94043",
                "short_name" : "94043",
                "types" : [ "postal_code" ]
             }
          ],
          "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
          "geometry" : {
             "location" : {
                "lat" : 37.4267861,
                "lng" : -122.0806032
             },
             "location_type" : "ROOFTOP",
             "viewport" : {
                "northeast" : {
                   "lat" : 37.4281350802915,
                   "lng" : -122.0792542197085
                },
                "southwest" : {
                   "lat" : 37.4254371197085,
                   "lng" : -122.0819521802915
                }
             }
          },
          "place_id" : "ChIJtYuu0V25j4ARwu5e4wwRYgE",
          "plus_code" : {
             "compound_code" : "CWC8+R3 Mountain View, California, United States",
             "global_code" : "849VCWC8+R3"
          },
          "types" : [ "street_address" ]
       }
    ],
    "status" : "OK"
}

Code:

DECLARE @json NVarChar(max) = N'{
    "results" : [
       {
          "address_components" : [
             {
                "long_name" : "1600",
                "short_name" : "1600",
                "types" : [ "street_number" ]
             },
             {
                "long_name" : "Amphitheatre Parkway",
                "short_name" : "Amphitheatre Pkwy",
                "types" : [ "route" ]
             },
             {
                "long_name" : "Mountain View",
                "short_name" : "Mountain View",
                "types" : [ "locality", "political" ]
             },
             {
                "long_name" : "Santa Clara County",
                "short_name" : "Santa Clara County",
                "types" : [ "administrative_area_level_2", "political" ]
             },
             {
                "long_name" : "California",
                "short_name" : "CA",
                "types" : [ "administrative_area_level_1", "political" ]
             },
             {
                "long_name" : "United States",
                "short_name" : "US",
                "types" : [ "country", "political" ]
             },
             {
                "long_name" : "94043",
                "short_name" : "94043",
                "types" : [ "postal_code" ]
             }
          ],
          "formatted_address" : "1600 Amphitheatre Pkwy, Mountain View, CA 94043, USA",
          "geometry" : {
             "location" : {
                "lat" : 37.4267861,
                "lng" : -122.0806032
             },
             "location_type" : "ROOFTOP",
             "viewport" : {
                "northeast" : {
                   "lat" : 37.4281350802915,
                   "lng" : -122.0792542197085
                },
                "southwest" : {
                   "lat" : 37.4254371197085,
                   "lng" : -122.0819521802915
                }
             }
          },
          "place_id" : "ChIJtYuu0V25j4ARwu5e4wwRYgE",
          "plus_code" : {
             "compound_code" : "CWC8+R3 Mountain View, California, United States",
             "global_code" : "849VCWC8+R3"
          },
          "types" : [ "street_address" ]
       }
    ],
    "status" : "OK"
}';

SELECT * 
FROM OpenJson(@json)
WITH 
    (Address NVARCHAR(200) '$results.formatted_address' ,
     latitude nvarchar(max) '$results.geometry.location.lat', 
     longitude nvarchar(max) '$results.geometry.location.lng') AS my_json;
0

There are 0 answers