Difference between revisions of "SimpleGraph-JSON"

From BITPlan Wiki
Jump to navigation Jump to search
Line 13: Line 13:
 
|viewmode=hidden
 
|viewmode=hidden
 
}}
 
}}
=Freitext=
 
 
== Example ==
 
== Example ==
 
=== Goal ===
 
=== Goal ===

Revision as of 10:36, 21 February 2018

SimpleGraphModule

JSON vector logo.svg

SimpleGraph JSON module

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

Sources

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

JUnit Test for SimpleGraph JSON module

We would like to get the JSON nodes in the raw result below that have the latitude information. We are looking for nodes with the name "location" and the property "lat". The line

  dom.g().V().hasLabel("location").forEachRemaining(v->latHolder.add(v.property("lat").value().toString()));
    String lat=latHolder.getFirstValue();

Does the work for us by traversing the JSON parser graph created with the two lines:

  JsonSystem js = new JsonSystem();
  SimpleSystem dom = js.connect("json", json);

JUnit Test case

@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"));
  }

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"
}