Note that there are some explanatory texts on larger screens.

plurals
  1. POextract map from vertex and return an array of maps via REST-API
    primarykey
    data
    text
    <p>I am trying to put all properties of a vertex in a map and add additional values to this map. Later, I want to return this map as a JSON response. In general I find the <a href="http://docs.neo4j.org/chunked/stable/gremlin-plugin.html" rel="nofollow">REST-API</a> of neo4j a bit verbose and I need the ID of a vertex among other values. Therefore, I decided to do this directly via a gremlin query.</p> <p>The result should look like:</p> <pre><code>[ {"id": 1, "name": "Name1"}, {"id": 2, "name": "Name2"} ] </code></pre> <p>I managed to do this using the following gremlin script:</p> <pre><code>x = []; g.v( 1 ).out.transform{ m = [:]; m.putAll( it.map() ); m.put( "id", it.id ); x.add( m ) }.iterate(); x </code></pre> <p>However, I ran into issues with this query with neo4j community 1.8.2 and 1.9M05.</p> <p>a) neo4j 1.8.2 returns a JSONArray of Strings instead of a JSONArray of JSONObjects:</p> <pre><code>[ "{id: 1, name: Name1}", "{id: 2, name: Name2}" ] </code></pre> <p>b) neo4j 1.9M5 returns an exception:</p> <pre><code>{ "message":"Invalid list type: map", "exception":"IllegalStateException", "stacktrace": [ "org.neo4j.server.rest.repr.RepresentationFormat.serializeList(RepresentationFormat.java:65)", "org.neo4j.server.rest.repr.ListRepresentation.serialize(ListRepresentation.java:50)", "org.neo4j.server.rest.repr.OutputFormat.assemble(OutputFormat.java:179)", "org.neo4j.server.rest.repr.OutputFormat.formatRepresentation(OutputFormat.java:131)", "org.neo4j.server.rest.repr.OutputFormat.response(OutputFormat.java:117)", "org.neo4j.server.rest.repr.OutputFormat.ok(OutputFormat.java:55)", "org.neo4j.server.rest.web.ExtensionService.invokeGraphDatabaseExtension(ExtensionService.java:122)", "java.lang.reflect.Method.invoke(Method.java:597)" ] } </code></pre> <p>If I modify the gremlin script and change x to a map, it works in both neo4j versions:</p> <pre><code>x = [:]; g.v( 1 ).out.transform{ m = [:]; m.putAll( it.map() ); m.put( "id", it.id ); x.put(it.id, m ); }.iterate(); x </code></pre> <p>returns</p> <pre><code>{ "1" : {"id": 1, "name": "Name1"}, "2" : {"id": 2, "name": "Name2"} } </code></pre> <p>the results, however, is now a JSONObject with JSONObjects in it.</p> <p>Is there a way to solve my problem using gremlin? I just started learning gremlin two days ago.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload