wonderful world
wonderful world

Reputation: 11599

How to transform a Gremlin GraphSON in tree format to custom JSON tree format in CosmosDB?

I have the following Gremlin queries that ran successfully in CosmosDB:

g.addV('person').property(id, 'grand_father').property('name', 'Grand Father')
g.addV('person').property(id, 'father').property('name', 'Father')
g.addV('person').property(id, 'child').property('name', 'Child')
g.V('grand_father').addE('father_of').to(V('father'))
g.V('father').addE('father_of').to(V('child'))

I ran the query g.V('grand_father').repeat(out()).emit().tree() which generates the following output:

[
  {
    "grand_father": {
      "key": {
        "id": "grand_father",
        "label": "person",
        "type": "vertex",
        "properties": {
          "name": [
            {
              "id": "2b687c65-6490-4846-a5ef-1b7d67e51916",
              "value": "Grand Father"
            }
          ]
        }
      },
      "value": {
        "father": {
          "key": {
            "id": "father",
            "label": "person",
            "type": "vertex",
            "properties": {
              "name": [
                {
                  "id": "c1f75463-8aa5-4c15-854d-88be0ec9cdc9",
                  "value": "Father"
                }
              ]
            }
          },
          "value": {
            "child": {
              "key": {
                "id": "child",
                "label": "person",
                "type": "vertex",
                "properties": {
                  "name": [
                    {
                      "id": "d74d6286-5fa9-4b90-9619-1f173d5da53e",
                      "value": "Child"
                    }
                  ]
                }
              },
              "value": {}
            }
          }
        }
      }
    }
  }
]

I want to transform the above GraphSON tree again to generate a custom hierarchical tree of the following format.

{
   "person":{
      "name":"Grand Father"
   },
   "children":[
      {
         "person":{
            "name":"Father"
         },
         "children":[
            {
               "person":{
                  "name":"Child"
               }
            }
         ]
      }
   ]
}

What changes that I need to make to g.V('grand_father').repeat(out()).emit().tree() to achieve the result?

Upvotes: 5

Views: 1297

Answers (1)

stephen mallette
stephen mallette

Reputation: 46226

I think that the following gets pretty close to what you're asking:

gremlin> g.V('grand_father').
......1>   repeat(out()).
......2>     emit().
......3>   tree().
......4>     by(group().
......5>          by(label).
......6>          by(valueMap('name').by(unfold())).
......7>        unfold().unfold())
==>[person={name=Grand Father}:[person={name=Father}:[person={name=Child}:[]]]]

Note that tree() takes a by() modulator which applies the anonymous traversal given to it as an argument to each item of the tree. I didn't capture the "children" leaf in your JSON output, but perhaps this gets you close enough to your target without introducing more complexity. Note that CosmosDB might not yet support the valueMap('name').by(unfold()) trick in which case you can remove the by(unfold()) and be left with List wrapping the value, replace it with project('name').by('name'), or replace it with the "old" way of unfolding a valueMap() shown next:

gremlin> g.V('grand_father').
......1>   repeat(out()).
......2>     emit().
......3>   tree().
......4>     by(group().
......5>          by(label).
......6>          by(valueMap('name').
......7>             unfold().
......8>             group().
......9>               by(select(keys)).
.....10>               by(select(values).unfold())).
.....11>        unfold().unfold())
==>[person={name=Grand Father}:[person={name=Father}:[person={name=Child}:[]]]]

Upvotes: 2

Related Questions