dj_coder dj_coder - 4 months ago 13
JSON Question

Print Netsted object's properties

I need to print postal_code value from below json file,

{
"results" : [
{
"address_components" : [
{
"long_name" : "286",
"short_name" : "286",
"types" : [ "street_number" ]
},
{
"long_name" : "West El Camino Real",
"short_name" : "W El Camino Real",
"types" : [ "route" ]
},
{
"long_name" : "Old Mountain View",
"short_name" : "Old Mountain View",
"types" : [ "neighborhood", "political" ]
},
{
"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" : "94040",
"short_name" : "94040",
"types" : [ "postal_code" ]
},
{
"long_name" : "2606",
"short_name" : "2606",
"types" : [ "postal_code_suffix" ]
}
],
"formatted_address" : "286 W El Camino Real, Mountain View, CA 94040, USA",
"geometry" : {
"location" : {
"lat" : 37.3833211,
"lng" : -122.0782706
},
"location_type" : "ROOFTOP",
"viewport" : {
"northeast" : {
"lat" : 37.38467008029149,
"lng" : -122.0769216197085
},
"southwest" : {
"lat" : 37.3819721197085,
"lng" : -122.0796195802915
}
}
},
"place_id" : "ChIJieTN_yu3j4AR6cF-aEhdc58",
"types" : [ "street_address" ]
}

"status" : "OK"
}


I have write following code in order to get and print response.

$.ajax({
dataType: "json",
url: 'https://maps.googleapis.com/maps/api/geocode/json?latlng=37.383253,-122.078075&sensor=false',
data: latlog,
success: function (response) {
$("#response").html(JSON.stringify(response["results"], null, 4));
}
});


This prints almost entire Object. How to print only postal_code value which is 94040?

Answer

Your JSON is wrong, check the demo code to see the right syntax.

How to print only postal_code value which is 94040?

You need to access the address component and filter out the object which has the type as postal_code

try this

obj.results[0].address_components.filter( function(obj){ 
  return obj.types.indexOf("postal_code") != -1 
})[0].long_name

DEMO

var obj = {
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "286",
               "short_name" : "286",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "West El Camino Real",
               "short_name" : "W El Camino Real",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Old Mountain View",
               "short_name" : "Old Mountain View",
               "types" : [ "neighborhood", "political" ]
            },
            {
               "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" : "94040",
               "short_name" : "94040",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "2606",
               "short_name" : "2606",
               "types" : [ "postal_code_suffix" ]
            }
         ],
         "formatted_address" : "286 W El Camino Real, Mountain View, CA 94040, USA",
         "geometry" : {
            "location" : {
               "lat" : 37.3833211,
               "lng" : -122.0782706
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 37.38467008029149,
                  "lng" : -122.0769216197085
               },
               "southwest" : {
                  "lat" : 37.3819721197085,
                  "lng" : -122.0796195802915
               }
            }
         },
         "place_id" : "ChIJieTN_yu3j4AR6cF-aEhdc58",
         "types" : [ "street_address" ]
      }],

   "status" : "OK"
}  

var long_name = obj.results[0].address_components.filter( function(obj){ return obj.types.indexOf("postal_code") != -1 } )[0].long_name;

console.log(long_name);

Comments