Taking out a record from JSON format on specific position

170 views Asked by At

I'm using google's geocode API to fetch data based on ZIP code that I pass like following:

http://maps.googleapis.com/maps/api/geocode/json?address=97001&sensor=true

I get a result like this:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "97001",
               "short_name" : "97001",
               "types" : [ "postal_code" ]
            },
            {
               "long_name" : "Antelope",
               "short_name" : "Antelope",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Oregon",
               "short_name" : "OR",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "United States",
               "short_name" : "US",
               "types" : [ "country", "political" ]
            }
         ],
         "formatted_address" : "Antelope, OR 97001, USA",
         "geometry" : {
            "bounds" : {
               "northeast" : {
                  "lat" : 45.0835979,
                  "lng" : -120.476616
               },
               "southwest" : {
                  "lat" : 44.801399,
                  "lng" : -120.91835
               }
            },
            "location" : {
               "lat" : 44.9552895,
               "lng" : -120.6265837
            },
            "location_type" : "APPROXIMATE",
            "viewport" : {
               "northeast" : {
                  "lat" : 45.0835979,
                  "lng" : -120.476616
               },
               "southwest" : {
                  "lat" : 44.801399,
                  "lng" : -120.91835
               }
            }
         },
         "place_id" : "ChIJhZMDGc0ovFQRBev1yy22nAk",
         "types" : [ "postal_code" ]
      }
   ],
   "status" : "OK"
}

Now this is good, but I'd like to pick out only the record from "addres_component" where types equals to:

"types" : [ "administrative_area_level_1", "political" ]

I can do something like this to fetch the value:

JObject.Parse(myJson)["keyhere"].ToString();

But how do I actually check whether the type for the values short_name and long_name is the one that I put above??

How can I achieve this using JObject class?

1

There are 1 answers

0
mtaanquist On BEST ANSWER

There is a method in Newtonsoft JSON called SelectToken, which allows you to use JSONPath (similar to XPath) to find specific parts of your JSON.

You can read more here: http://www.newtonsoft.com/json/help/html/SelectToken.htm

For your specific case, you could do something like this:

JObject o = JObject.Parse(json); 
JToken token = o.SelectToken("$..address_components[?(@.types.indexOf('administrative_area_level_1') != -1), ?(.types.indexOf('political') != -1)]");

This is not tested, and you might have to fiddle about with it yourself. Documentation is available in the link to Newtonsoft JSON.

The evaluation results of the above JSONPath gave the following:

'0' ...
  'long_name' => "Oregon"
  'short_name' => "OR"
  'types' ...
    '0' => "administrative_area_level_1"
    '1' => "political"

Some notes:

Inside the ?() test in the JSONPath you can use JavaScript. See details here: https://stackoverflow.com/a/30031333/1862405

The union character , acts like an OR. There are no logical operators in JSONPath yet. Read more here: https://stackoverflow.com/a/23897735/1862405

For testing your JSONPath, you can use an online tester. I used http://jsonpath.com/