Cardinality-aware value mapping? Return SINGLE values without brackets, but LIST values as arrays


Patrick S <prstr...@...>
 

This may be more of a Gremlin question but...

I'm looking for a way to return property values in the proper format based on their Cardinality (LIST/SET vs SINGLE, returning them in brackets vs. without brackets).

For example, say I have a vertex with two properties on it: "Name" and "BingoNumbers". Name is defined in my JanusGraph schema with "SINGLE" Cardinality.
BingoBumbers is "LIST" Cardinality. 

If I query this vertex with g.V(myId).valueMap(), I get back something like:
{ "Name": [ "Alice" ], "BingoNumbers: [ "B1", "I9", "G12" ] }

What I would like instead is:
{ "Name": "Alice", "BingoNumbers: [ "B1", "I9", "G12" ] }

Currently to get that result, I need to loop through the gremlin results, reference my graph schema for each property on each vertex, and extract the first array element for SINGLE properties while leaving LIST properties as is. 

The closest I found to a gremlin workaround for this is here: 
g.V(myId).valueMap().map(unfold().group().by(keys).by(choose(select(values).count(local).is(1), select(values).unfold(), select(values))));
But this misses the case where a LIST property can have a single element, but should still be returned as a List. For example: { "Name": "Alice", "BingoNumbers: [ "N7" ] } would be incorrectly returned as { "Name": "Alice", "BingoNumbers: "N7" }.

Has anyone else faced this problem, or know any gremlin/janusgraph solution that correctly accounts for Cardinality?