Search code examples
gremlinjanusgraph

Gremlin valueMap() all values inside lists


Say I do g.V().has("id", 1).valueMap().next()

The result is all in lists:

{
  "id": [1],
  "name" ["node1"]
}

How can I unfold all the inner lists so that it shows:

{
  "id": 1,
  "name" "node1"
}

Solution

  • I think you already answered your question in a sense - you use unfold()

    g.V().has("id",1).
      valueMap().
        by(unfold())
    

    That syntax only works on 3.4.0 when by() modulator was added to valueMap(). In earlier versions you can still do it but it's not as pretty:

    g.V().has("id",1).
      valueMap().
      unfold().
      group().
        by(keys).
        by(select(values).unfold())
    

    As you can see, you basically have to deconstruct the Map and then reconstruct it with group(). If you have multiple vertices you need to isolate the unfold() and so:

    g.V().
      map(valueMap().
          unfold().
          group().
            by(keys).
            by(select(values).unfold()))