Converting results to JSON using GraphSONWriter.

1,743 views
Skip to first unread message

Jim Lloyd

unread,
Oct 25, 2014, 6:57:32 PM10/25/14
to gremli...@googlegroups.com
I'm working on upgrading gremlin-node to TinkerPop3 and would like some guidance about the best way to obtain JSON results from gremlin queries. Since gremlin-node uses Javascript, JSON is the ideal format for returning results. With TinkerPop2, gremlin-node used com.tinkerpop.rexster.gremlin.converter.JSONResultConverter which worked well. It appears that there is no direct corollary to JSONResultConverter in TK3, correct?

I see that TinkerPop3 has com.tinkerpop.gremlin.structure.io.graphson, and specifically the class GraphSONWriter, which has methods such as writeVertices(OutputStream outputStream, Traversal<?,Vertex> traversal). On the surface this looks perfect, as the general pattern should be that a gremlin statement encodes Traversal composed of a series of steps, the final result of which should be output as a JSON stream. But there are a couple hurdles that I need to overcome:

1) It seems that GraphSONWriter only has a means of writing the results of a Traversal that yields Vertices. What about queries that return edges, such as g.E().has(...)?

2) It seems that writeVertices returns results that are not valid JSON. Instead of an array of objects, I'm seeing a stream of multiple objects (one per vertex), each encoded in braces, without the other syntax required to treat them as an array. Something like this:

{"id":1,"label":"vertex", ... }{"id":2,"label":"vertex", ... }{"id":3,"label":"vertex", ... }

I expect I can work around these problems by writing additional code. For the first problem, I can iterate over the edges and call writeEdge(OutputStream outputStream, Edge e) for each edge, along with the necessary syntax to create a JSON array. If I do that, then I probably should just do the same thing for vertices. But it seems wrong to do this when GraphSONWriter already has methods for handling Traversals. Yet if I use writeVertices as is, I will need to parse the stream to insert the array syntax, which is likely going to be about the same level of effort.

Am I overlooking a better solution? Is GraphSONWriter's current behavior working as designed? Although it may be too late to change or enhance GraphSONWriter's for TK 3.0 GM, does it make sense to consider enhancements for a subsequent minor release?

Thanks.



Stephen Mallette

unread,
Oct 26, 2014, 1:49:05 AM10/26/14
to gremli...@googlegroups.com
GraphSONWriter doesn't produce valid JSON as a total file.  It produce valid "lines" of JSON.  In this way, the file can be sliced up into pieces to be easily processed in parallel.  That's not really the use case you are looking for.  What I think you want to do is to construct an instance of this class:


something like:

mapper = GraphSONObjectMapper.build()
            .embedTypes(false)
            .create();

from which you can then call the standard Jackson serialization methods like:

mapper.writeValueAsString(somethingToSerialize)



.

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-user...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/CAHqfici1h4fd28d9jTNPuBJCfjb4_%3DMmquGbobnt_3GU3yj5sQ%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.

Jim Lloyd

unread,
Oct 27, 2014, 5:04:23 PM10/27/14
to gremli...@googlegroups.com
Thanks Stephen. I don't have prior exposure to Jackson, which caused me to overlook this approach.

Jim

ankita singh

unread,
May 28, 2018, 11:11:11 PM5/28/18
to Gremlin-users

Hello!


I'm currently working on application ,which finds shortest path between two provided nodes.the gremlin query which I wrote works well in gremlin console,but when it is parsed to convert it into json using websocket returns error.Kindly help me out of this .
My gremlin query is as follows:-
g.V().has('name',' ').repeat(out().simplepath()).until(has('name',' ')).path().limit(1).
But this query isn;t working well with json.
Actually I have to visualize that shortest path using results of Json,and I'm unable to do that,kindly help me out of this.

Stephen Mallette

unread,
May 29, 2018, 7:01:36 AM5/29/18
to Gremlin-users
what's the error?

--
You received this message because you are subscribed to the Google Groups "Gremlin-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email to gremlin-users+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/gremlin-users/2d8f6646-f5b7-43ba-afce-e97db59269de%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages