1

I am trying to convert the output of gremlin console into json format,

For Ex : gremlin>g.V(409608).valueMap()

sample OUTPUT : [eName:[FS-BR-GOJU-ENB-G001_MW],lng:[000086.2119100],modulation:[2048],city:[Jamshedpur],hopType:[1+0],eType:[MICROWAVE],cTime:[Sat Mar 03 20:37:27 IST 2018],aendSapId:[FS-BR-JMDP-ENB-6005],vendor:[CERAGON],domain:[MW],location:[POINT (86.21191 22.79906)],state:[Jharkhand],mTime:[Sat Mar 03 20:37:27 IST 2018],lat:[000022.7990600],sapId:[FS-BR-GOJU-ENB-G001]]

how can i convert it into json ??

This "g.V(409608).valueMap()" is just an example, i was looking for a function/way that can convert any type of query output into json.

Actually i am trying to develop a feature in which i have a gremlin query as string ("gremlin query") and i need its output in JSON (USING JAVA).

Muhammad Omer Aslam
  • 22,976
  • 9
  • 42
  • 68

1 Answers1

5

You can serialize to GraphSON if you create a Jackson ObjectMapper:

gremlin> mapper = GraphSONMapper.build().version(GraphSONVersion.V3_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@7e97551f
gremlin> v = g.V().has('name','marko').valueMap(true).next()
==>id=1
==>name=[marko]
==>age=[29]
==>label=person
gremlin> mapper.writeValueAsString(v)
==>{"@type":"g:Map","@value":[{"@type":"g:T","@value":"id"},{"@type":"g:Int32","@value":1},"name",{"@type":"g:List","@value":["marko"]},"age",{"@type":"g:List","@value":[{"@type":"g:Int32","@value":29}]},{"@type":"g:T","@value":"label"},"person"]}

If you want JSON with non-embedded types, you could use GraphSON 1.0

gremlin> mapper = GraphSONMapper.build().version(GraphSONVersion.V1_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper@4a3be6a5
gremlin> mapper.writeValueAsString(v)
==>{"id":1,"name":["marko"],"age":[29],"label":"person"}
stephen mallette
  • 45,298
  • 5
  • 67
  • 135
  • Thank You, it was very helpful. Do you have any idea regarding last part of the question i.e convert a gremlin query "query in string" --> JSON (IN JAVA) – TechFramer - Sanil Bagzai Mar 21 '18 at 11:41
  • Gremlin Server already does that. It accepts gremlin strings and returns results as GraphSON. You could embed Gremlin Server in your application. If that is too heavy then I guess you could embed just the `GremlinGroovyScriptEngine` to `eval()` your string to get a result which you could then convert to JSON however you wanted. – stephen mallette Mar 21 '18 at 13:01
  • Where do I set `GraphSONVersion.V1_0` for JanusGraph Server? – Phani Jan 25 '19 at 14:39
  • @stephenmallette I've added a new question: https://stackoverflow.com/questions/54367581/where-to-configure-graphsonversion-v1-0-for-janusgraph-gremlin-server for this effect. Kindly help me – Phani Jan 25 '19 at 14:49
  • @stephenmallette Is it possible to print Neptune response in GraphSON format? – Sanjay Sharma Feb 03 '22 at 10:38
  • The TinkerPop drivers tend to hide serialization concerns (e.g. graphson) away from users, so you really just work with the native objects in your programming language. That's typically the most convenient way to work with results. You could always pass the result you get back through a GraphSON serializer i suppose, but depending on the driver it might be hard to get access to the raw form serialized from Neptune (or any other graph). If you did get it to work somehow it wouldn't likely be a supported usage pattern. – stephen mallette Feb 03 '22 at 14:54