SimpleGraph-JSON

From BITPlan Wiki
Revision as of 09:10, 14 February 2018 by Wf (talk | contribs) (→‎Explanation)
Jump to navigation Jump to search

SimpleGraph JSON module

The SimpleGraph JSON module makes JSON parse results accessible to Graph processing.

Example

Goal

We would like to retrieve the latitute of cologne cathedral from the Google Maps API. The query returns JSON data for the location of cologne cathedral.

Result

50.9412

Explanation

Raw Result

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "4",
               "short_name" : "4",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Domkloster",
               "short_name" : "Domkloster",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Innenstadt",
               "short_name" : "Innenstadt",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Köln",
               "short_name" : "Köln",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Köln",
               "short_name" : "K",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Nordrhein-Westfalen",
               "short_name" : "NRW",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Deutschland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "50667",
               "short_name" : "50667",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Domkloster 4, 50667 Köln, Deutschland",
         "geometry" : {
            "location" : {
               "lat" : 50.94127839999999,
               "lng" : 6.9582814
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 50.94262738029149,
                  "lng" : 6.959630380291502
               },
               "southwest" : {
                  "lat" : 50.93992941970849,
                  "lng" : 6.956932419708497
               }
            }
         },
         "place_id" : "ChIJLz2cNqUlv0cRxqnjljiR7Ck",
         "types" : [ "church", "establishment", "place_of_worship", "point_of_interest" ]
      },
      {
         "address_components" : [
            {
               "long_name" : "4",
               "short_name" : "4",
               "types" : [ "street_number" ]
            },
            {
               "long_name" : "Domkloster",
               "short_name" : "Domkloster",
               "types" : [ "route" ]
            },
            {
               "long_name" : "Innenstadt",
               "short_name" : "Innenstadt",
               "types" : [ "political", "sublocality", "sublocality_level_1" ]
            },
            {
               "long_name" : "Köln",
               "short_name" : "Köln",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Köln",
               "short_name" : "K",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Nordrhein-Westfalen",
               "short_name" : "NRW",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Deutschland",
               "short_name" : "DE",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "50667",
               "short_name" : "50667",
               "types" : [ "postal_code" ]
            }
         ],
         "formatted_address" : "Domkloster 4, 50667 Köln, Deutschland",
         "geometry" : {
            "location" : {
               "lat" : 50.9412892,
               "lng" : 6.958912499999999
            },
            "location_type" : "ROOFTOP",
            "viewport" : {
               "northeast" : {
                  "lat" : 50.9426381802915,
                  "lng" : 6.960261480291502
               },
               "southwest" : {
                  "lat" : 50.9399402197085,
                  "lng" : 6.957563519708498
               }
            }
         },
         "place_id" : "ChIJ-ydP-a8lv0cRfBO-EehI_1s",
         "types" : [ "church", "establishment", "place_of_worship", "point_of_interest" ]
      }
   ],
   "status" : "OK"
}

JUnit Test for SimpleGraph JSON module

@Test
  public void testGoogleMapsJsonApi() throws Exception {
    String url="https://maps.googleapis.com/maps/api/geocode/json?address=Cologne%20Cathedral";
    String json=IOUtils.toString(new URL(url),"UTF-8");
    debug=true;
    if (debug)
      System.out.println(json);
    JsonSystem js = new JsonSystem();
    SimpleSystem dom = js.connect("json", json);
    if (debug)
      js.getStartNode().forAll(SimpleNode.printDebug);
    Holder<String> latHolder=new Holder<String>();
    dom.g().V().hasLabel("location").forEachRemaining(v->latHolder.add(v.property("lat").value().toString()));
    String lat=latHolder.getFirstValue();
    if (debug)
      System.out.println(lat);
    assertTrue(lat.startsWith("50.9412"));
  }