Difference between revisions of "SimpleGraph-JSON"

From BITPlan Wiki
Jump to navigation Jump to search
(Created page with "= SimpleGraph JSON module = The {{Link|target=SimpleGraph}} JSON module makes JSON parse results accessible to Graph processing. == Example == === Goal === === Result === ==...")
 
 
(15 intermediate revisions by 2 users not shown)
Line 1: Line 1:
= SimpleGraph JSON module =
+
{{SimpleGraphModule
The {{Link|target=SimpleGraph}} JSON module makes JSON parse results accessible to Graph processing.
+
|name=JSON
 +
|logo=File:JSON vector logo.svg
 +
|modulename=json
 +
|systemname=JsonSystem
 +
|url=https://en.wikipedia.org/wiki/JavaScript_Object_Notation
 +
|apiname=JSON
 +
|apiurl=https://www.json.org/
 +
|documentation=makes JSON parse results accessible to Graph processing.
 +
|storemode=property
 +
|viewmode=hidden
 +
}}
 +
== Example ==
 +
=== Goal ===
 +
We would like to retrieve the latitute of cologne cathedral from the [https://developers.google.com/maps/documentation/geocoding/intro Google Maps API].
 +
The query returns [https://maps.googleapis.com/maps/api/geocode/json?address=Cologne%20Cathedral JSON data for the location of cologne cathedral].
  
== Example ==
 
=== Goal ===
 
 
=== Result ===
 
=== Result ===
 +
50.9412
 +
 
=== Explanation ===
 
=== 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
 +
<source lang='java'>
 +
  dom.g().V().hasLabel("location").forEachRemaining(v->latHolder.add(v.property("lat").value().toString()));
 +
    String lat=latHolder.getFirstValue();
 +
</source>
 +
Does the work for us by traversing the JSON parser graph created with the two lines:
 +
<source lang='java'>
 +
  JsonSystem js = new JsonSystem();
 +
  SimpleSystem dom = js.connect("json", json);
 +
</source>
 +
=== JUnit Test case ===
 +
<source lang='java'>
 +
@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"));
 +
  }
 +
</source>
 +
==== Raw Result ====
 +
<source lang='json'>
 +
{
 +
  "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"
 +
}
 +
</source>
 +
 
[[Category:frontend]]
 
[[Category:frontend]]
 
[[Category:SimpleGraph]]
 
[[Category:SimpleGraph]]

Latest revision as of 06:38, 1 September 2018

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