Object Construction with nested m:n structure in jq - jq

I have this json
{
"objectEntries": [
{
"objectType": {
"name": "Test"
},
"attributes": [
{
"id": 16,
"objectTypeAttributeId": 8,
"objectAttributeValues": [
{
"referencedObject": {
"id": 17,
"label": "TestTest",
"attributes": [
{
"id": 20,
"objectTypeAttributeId": 11,
"objectAttributeValues": [
{
"value": "bli"
}
],
"objectId": 12
},
{
"id": 21,
"objectTypeAttributeId": 13,
"objectAttributeValues": [
{
"value": "blo"
}
],
"objectId": 14
},
{
"id": 22,
"objectTypeAttributeId": 15,
"objectAttributeValues": [
{
"referencedObject": {
"id": 30,
"label": "TestTest",
"attributes": [
{
"id": 35,
"objectTypeAttributeId": 36,
"objectAttributeValues": [
{
"value": "bli"
}
],
"objectId": 37
},
{
"id": 38,
"objectTypeAttributeId": 39,
"objectAttributeValues": [
{
"value": "blo"
}
],
"objectId": 40
}
]
}
}
],
"objectId": 16
}
]
}
}
]
},
{
"id": 16,
"objectTypeAttributeId": 8,
"objectAttributeValues": [
{
"referencedObject": {
"id": 17,
"label": "TestTest",
"attributes": [
{
"id": 20,
"objectTypeAttributeId": 11,
"objectAttributeValues": [
{
"value": "bli"
}
],
"objectId": 12
},
{
"id": 21,
"objectTypeAttributeId": 13,
"objectAttributeValues": [
{
"value": "blo"
}
],
"objectId": 14
},
{
"id": 22,
"objectTypeAttributeId": 15,
"objectAttributeValues": [
{
"referencedObject": {
"id": 30,
"label": "TestTest",
"attributes": [
{
"id": 35,
"objectTypeAttributeId": 36,
"objectAttributeValues": [
{
"value": "xxx"
}
],
"objectId": 37
},
{
"id": 38,
"objectTypeAttributeId": 39,
"objectAttributeValues": [
{
"value": "yyy"
}
],
"objectId": 40
}
]
}
}
],
"objectId": 16
}
]
}
}
]
}
]
}
]
}
I want this result.
{
"name": "Test",
"attribute36": "bli",
"attribute39": "blo"
}
{
"name": "Test",
"attribute36": "xxx",
"attribute39": "yyy"
}
But what I get is this
{
"name": "Test",
"attribute36": "bli",
"attribute39": "blo"
}
{
"name": "Test",
"attribute36": "bli",
"attribute39": "yyy"
}
{
"name": "Test",
"attribute36": "xxx",
"attribute39": "blo"
}
{
"name": "Test",
"attribute36": "xxx",
"attribute39": "yyy"
}
The jq query I came up with
jq '.objectEntries[] | {"name": .objectType.name, "attribute36": .attributes | .[] | select(.objectTypeAttributeId==8) | .objectAttributeValues[].referencedObject.attributes | .[] | select(.objectTypeAttributeId==15) | .objectAttributeValues[].referencedObject.attributes | .[] | select(.objectTypeAttributeId==36) | .objectAttributeValues | .[] | .value, "attribute39": .attributes | .[] | select(.objectTypeAttributeId==8) | .objectAttributeValues[].referencedObject.attributes | .[] | select(.objectTypeAttributeId==15) | .objectAttributeValues[].referencedObject.attributes | .[] | select(.objectTypeAttributeId==39) | .objectAttributeValues | .[] | .value}' stackoverflow.json
You can see the problem also on jqplay https://jqplay.org/s/NjqEy7wvQE
The goal is to extract certain fields on the different jsonpath depths into a new, flat json object, per toplevel json object, where toplevel are the objects under objectEntries[]

Looking at the jq query in the Q, we can define two useful functions:
def attributes:
[ .attributes[]
| select(.objectTypeAttributeId==8)
| .objectAttributeValues[].referencedObject.attributes[]
| select(.objectTypeAttributeId==15)
| .objectAttributeValues[].referencedObject.attributes[] ] ;
def to_o:
{ ("attribute" + (.objectTypeAttributeId|tostring)): first(.objectAttributeValues[].value)};
And we see that
.objectEntries[] | attributes[] | to_o
yields:
{"attribute36":"bli"}
{"attribute39":"blo"}
{"attribute36":"xxx"}
{"attribute39":"yyy"}
This is where the original question becomes a bit difficult to understand. I'm going to assume the intent is to group these objects together without loss of information and without introducing arrays.
At any rate, the following filter does produce the desired result:
.objectEntries[]
| .objectType.name as $name
| foreach ((attributes[] | to_o), null ) as $o ({};
if $o == null then .emit = .object
elif .object|has($o|keys_unsorted[0])
then .emit=.object | .object=$o
else .emit=null | .object += $o
end;
select(.emit) | .emit)
| {$name} + .

Related

HighCharts: align rotated multiline label

I'm trying to align axis labels in HighCharts
Labels should not be trimed, so ellipsis is not allowed
How it looks now:
How I want it to be:
Code: https://jsfiddle.net/9xw6dL3f/25/
{
"chart": {
"width": 500,
"height": 300,
"type": "column"
},
"title": {
"text": null
},
"xAxis": {
"type": "category",
"labels": {
"rotation": -90,
"style": {
"textOverflow": "none"
}
}
},
"series": [
{
"name": "",
"data": [
{ "name": "Follow me into my theatre of lunacy", "y": -10, "color": "#ABC" },
{ "name": "1", "y": 1, "color": "#000" },
{ "name": "2", "y": 2, "color": "#000" },
{ "name": "3", "y": 3, "color": "#000" },
{ "name": "4", "y": 4, "color": "#000" },
{ "name": "5", "y": 5, "color": "#000" },
]
}
]
}
It is possible to move label depending on it's rows count
Add render function to the chart object
{
"chart": {
...
events: {
render() {
for (let i = 0; i < this.xAxis[0].names.length; i++) {
const label = this.xAxis[0].ticks[i].label;
const rows = (label.element.innerHTML.match(/<\/tspan>/g) || []).length;
label.translate(-8 * rows, 0);
}
}
}
where 8 is half of row size in px
https://jsfiddle.net/kLz1uoga/5/

Retrieve consecutive array element from an array using JSONPath without using index, only by their value

I have a json body as below -
{
"type": "SourceUnit",
"children": [
{
"type": "PragmaDirective",
"name": "solidity",
"value": ">=0.4.22 <0.9.0"
},
{
"type": "ContractDefinition",
"name": "txorigin",
"baseContracts": [],
"subNodes": [],
"kind": "contract"
}
],
"tokens": [
{
"type": "Keyword",
"value": "pragma"
},
{
"type": "Identifier",
"value": "solidity"
},
{
"type": "Punctuator",
"value": ">="
},
{
"type": "Version",
"value": "0.4.22"
},
{
"type": "Punctuator",
"value": "<"
},
{
"type": "Version",
"value": "0.9.0"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Keyword",
"value": "contract"
},
{
"type": "Identifier",
"value": "txorigin"
},
{
"type": "Punctuator",
"value": "{"
},
{
"type": "type",
"value": "address"
},
{
"type": "Identifier",
"value": "owner"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Keyword",
"value": "constructor"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Keyword",
"value": "public"
},
{
"type": "Punctuator",
"value": "{"
},
{
"type": "Identifier",
"value": "owner"
},
{
"type": "Punctuator",
"value": "="
},
{
"type": "Identifier",
"value": "msg"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "sender"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Punctuator",
"value": "}"
},
{
"type": "Keyword",
"value": "function"
},
{
"type": "Identifier",
"value": "sendTo"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "type",
"value": "address"
},
{
"type": "Identifier",
"value": "receiver"
},
{
"type": "Punctuator",
"value": ","
},
{
"type": "type",
"value": "uint"
},
{
"type": "Identifier",
"value": "amount"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Keyword",
"value": "public"
},
{
"type": "Punctuator",
"value": "{"
},
{
"type": "Identifier",
"value": "require"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Identifier",
"value": "tx"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "origin"
},
{
"type": "Punctuator",
"value": "=="
},
{
"type": "Identifier",
"value": "owner"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "type",
"value": "bool"
},
{
"type": "Identifier",
"value": "success"
},
{
"type": "Punctuator",
"value": ","
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": "="
},
{
"type": "Identifier",
"value": "receiver"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "call"
},
{
"type": "Punctuator",
"value": "."
},
{
"type": "Identifier",
"value": "value"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Identifier",
"value": "amount"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Keyword",
"value": ""
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Identifier",
"value": "require"
},
{
"type": "Punctuator",
"value": "("
},
{
"type": "Identifier",
"value": "success"
},
{
"type": "Punctuator",
"value": ")"
},
{
"type": "Punctuator",
"value": ";"
},
{
"type": "Punctuator",
"value": "}"
},
{
"type": "Punctuator",
"value": "}"
},
{
"type": "Keyword",
"value": "<EOF>"
}
]
}
whenever any code executes, I want to check that the above "tx", ".", "origin" values are available in my generated json or not. and, it should be consecutive like (tx.origin). if it is available it will return something. I am available to get any one value - .tokens[?(#.value == "tx")].value
I need to check all 3 in a consecutive manner.
Could you guys help me?
Thanks in advance.
Another option is to just get the values
$.tokens[*].value
This will get you all the values. They should be in the right sequence, but there's no guarantee.

Here Waypoint Sequencing API Doesn't Honor Delivery Windows

I am experimenting with the HERE Waypoint Sequencing API using the new JSON request format, and it doesn't seem to take into account delivery windows specified in the times array within the plan object. I went back to the example provided in the documentation and found that the same thing was happening: Arrival and departure times are far from the specified delivery windows.
In my Example, Customer 1 has times:
"times": [["2020-07-04T08:00:00Z","2020-07-04T10:00:00Z"],["2020-07-04T16:00:00Z","2020-07-04T18:00:00Z"]]
However, the solution contains:
"time": {
"arrival": "2020-07-04T00:30:00.000+0000",
"departure": "2020-07-04T00:33:00.000+0000"
}
Which isn't even close.
Endpoint: https://wps.hereapi.com/v8/findsequence
Request:
{
"configuration": {
"optimizations": {
"traffic": "liveOrHistorical",
"waitingTime": {
"bufferTime": 15,
"reduce": false
}
}
},
"fleet": {
"profiles": [{"type":"car","name":"normal_car"}],
"types": [
{
"amount": 1,
"capacity": [10,5],
"costs": {
"distance": 0.0001,
"fixed": 22,
"time": 0.0048
},
"id": "string",
"limits": {
"maxDistance": 20000,
"shiftTime": 21600
},
"profile": "normal_car",
"shifts": [
{
"breaks": [
{
"duration": 180,
"location": {
"lat": 52.53088,
"lng": 13.38471
},
"times": [["2020-07-04T08:00:00Z","2020-07-04T10:00:00Z"],["2020-07-04T16:00:00Z","2020-07-04T18:00:00Z"]]
}
],
"end": {
"location": {
"lat": 52.53088,
"lng": 13.38471
},
"time": "2020-07-04T23:30:00Z"
},
"start": {
"location": {
"lat": 52.53088,
"lng": 13.38471
},
"time": "2020-07-04T00:30:00Z"
}
}
],
"skills": ["fridge"]
}
]
},
"id": "7f3423c2-784a-4983-b472-e14107d5a54a",
"plan": {
"jobs": [
{
"id": "string",
"places": {
"deliveries": [
{
"demand": [10,5],
"duration": 180,
"location": {
"lat": 52.53088,
"lng": 13.38471
},
"tag": "Customer 1",
"times": [["2020-07-04T08:00:00Z","2020-07-04T10:00:00Z"],["2020-07-04T16:00:00Z","2020-07-04T18:00:00Z"]]
},
{
"demand": [10,5],
"duration": 180,
"location": {
"lat": 53.53088,
"lng": 14.98471
},
"tag": "Customer 2",
"times": [["2020-07-04T08:00:00Z","2020-07-04T10:00:00Z"],["2020-07-04T16:00:00Z","2020-07-04T18:00:00Z"]]
}
],
"pickups": [
{
"demand": [10,5],
"duration": 180,
"location": {
"lat": 52.53088,
"lng": 13.98471
},
"tag": "some_tag",
"times": [["2020-07-04T08:00:00Z","2020-07-04T10:00:00Z"],["2020-07-04T16:00:00Z","2020-07-04T18:00:00Z"]]
}
]
},
"skills": ["fridge"]
}
],
"relations": [
{
"jobs": [
"string"
],
"type": "flexible",
"vehicleId": "7f3423c2-784a-4983-b472-e14107d5a54a"
}
]
}
}
Response:
{
"problemId": "7f3423c2-784a-4983-b472-e14107d5a54a",
"statistic": {
"cost": null,
"distance": 441655,
"duration": 20127,
"times": {
"break": 0,
"driving": 19587,
"serving": 540,
"waiting": 0
}
},
"tours": [
{
"vehicleId": "string",
"typeId": null,
"stops": [
{
"location": {
"lat": 52.53088,
"lng": 13.38471
},
"time": {
"arrival": null,
"departure": "2020-07-04T00:30:00.000+0000"
},
"load": [
],
"activities": [
{
"jobId": "departure",
"jobTag": "departure",
"type": null,
"location": null,
"time": null
}
]
},
{
"location": {
"lat": 52.53088,
"lng": 13.38471
},
"time": {
"arrival": "2020-07-04T00:30:00.000+0000",
"departure": "2020-07-04T00:33:00.000+0000"
},
"load": [
],
"activities": [
{
"jobId": "Customer 1",
"jobTag": "Customer 1",
"type": null,
"location": null,
"time": null
}
]
},
{
"location": {
"lat": 52.53088,
"lng": 13.98471
},
"time": {
"arrival": "2020-07-04T01:34:05.000+0000",
"departure": "2020-07-04T01:37:05.000+0000"
},
"load": [
],
"activities": [
{
"jobId": "some_tag",
"jobTag": "some_tag",
"type": null,
"location": null,
"time": null
}
]
},
{
"location": {
"lat": 53.53088,
"lng": 14.98471
},
"time": {
"arrival": "2020-07-04T03:53:37.000+0000",
"departure": "2020-07-04T03:56:37.000+0000"
},
"load": [
],
"activities": [
{
"jobId": "Customer 2",
"jobTag": "Customer 2",
"type": null,
"location": null,
"time": null
}
]
},
{
"location": {
"lat": 52.53088,
"lng": 13.38471
},
"time": {
"arrival": "2020-07-04T06:05:27.000+0000",
"departure": null
},
"load": [
],
"activities": [
{
"jobId": "arrival",
"jobTag": "arrival",
"type": null,
"location": null,
"time": null
}
]
}
],
"statistic": {
"cost": null,
"distance": 441655,
"duration": 20127,
"times": {
"break": 0,
"driving": 19587,
"serving": 540,
"waiting": 0
}
}
}
],
"unassigned": null
}
Am I missing something?

Address in autocomplete results

When I do an autosuggest request in the address response I only receive the label. The address object is not filled.
This is my request: https://autosuggest.search.hereapi.com/v1/autosuggest?at=48.8578817,2.2791249999999934&limit=5&q=stj&apikey=xxxx
I receive this:
"title": "Top Services (Stj)",
"id": "here:pds:place:250u09wj-afceace0a11b49bca950ea22d0f8e452",
"resultType": "place",
"address": {
"label": "********"
},
I will need to receive the formatted address without the name
How can I fix this?
thanks
We have tried to reproduce your query and it works for us, please see the following code.
Please review the documentation page for detailed information on autosuggest
Documentation link :
https://autosuggest.search.hereapi.com/v1/autosuggest?at=48.8578817,2.2791249999999934&limit=5&q=stj&apikey=XXXX.
Query Result:
{
"items": [
{
"title": "Top Services (Stj)",
"id": "here:pds:place:250u09wj-afceace0a11b49bca950ea22d0f8e452",
"resultType": "place",
"address": {
"label": "Top Services, 61 Rue du Faubourg Saint-Denis, 75010 Paris, France"
},
"position": {
"lat": 48.8721,
"lng": 2.35384
},
"access": [
{
"lat": 48.87208,
"lng": 2.35392
}
],
"distance": 5690,
"categories": [
{
"id": "700-7200-0254",
"name": "Construction",
"primary": true
},
{
"id": "700-7200-0264",
"name": "Services de conseil et de gestion"
},
{
"id": "700-7200-0277",
"name": "Entrepreneur de second œuvre"
}
],
"highlights": {
"title": [
{
"start": 14,
"end": 17
}
],
"address": {
"label": [
]
}
}
},
{
"title": "La Clinique du Scooter d'Occasion (Stj 2 Roues)",
"id": "here:pds:place:250u09tv-591de1b090c54199b11154a72f462c0d",
"resultType": "place",
"address": {
"label": "La Clinique du Scooter d'Occasion, 148 Boulevard du Montparnasse, 75014 Paris, France"
},
"position": {
"lat": 48.84039,
"lng": 2.33383
},
"access": [
{
"lat": 48.84052,
"lng": 2.33393
}
],
"distance": 4451,
"categories": [
{
"id": "400-4200-0241",
"name": "Services logistiques",
"primary": true
}
],
"highlights": {
"title": [
{
"start": 35,
"end": 38
}
],
"address": {
"label": [
]
}
}
},
{
"title": "Stjohn's",
"id": "here:pds:place:2501r4ve-43f913ba8f9b0e7046aaee61bcfe8b9b",
"resultType": "place",
"address": {
"label": "Stjohn's, 11 Rue Edmond Valentin, 75007 Paris, France"
},
"position": {
"lat": 48.85961,
"lng": 2.30159
},
"access": [
{
"lat": 48.85968,
"lng": 2.30157
}
],
"distance": 1655,
"categories": [
{
"id": "700-7400-0141",
"name": "Services aux entreprises",
"primary": true
},
{
"id": "700-7200-0252",
"name": "Publicité, marketing, communication et études de marché"
}
],
"highlights": {
"title": [
{
"start": 0,
"end": 3
}
],
"address": {
"label": [
{
"start": 0,
"end": 3
}
]
}
}
},
{
"title": "Stjohn S",
"id": "here:pds:place:250u09tv-0f38e06dd8634ab4a0d931dad94de24c",
"resultType": "place",
"address": {
"label": "Stjohn S, 4 Rue des Petits-Pères, 75002 Paris, France"
},
"position": {
"lat": 48.86601,
"lng": 2.34027
},
"access": [
{
"lat": 48.8662,
"lng": 2.34023
}
],
"distance": 4564,
"categories": [
{
"id": "700-7200-0252",
"name": "Publicité, marketing, communication et études de marché",
"primary": true
},
{
"id": "700-7200-0271",
"name": "Services financiers et d'assurance"
}
],
"highlights": {
"title": [
{
"start": 0,
"end": 3
}
],
"address": {
"label": [
{
"start": 0,
"end": 3
}
]
}
}
},
{
"title": "Espace St-Jo'",
"id": "here:pds:place:250u09te-220742c64e4a40a48b8772987e7f987c",
"resultType": "place",
"address": {
"label": "Espace St-Jo', 54 Rue du Moulin de Pierres, 92140 Clamart, France"
},
"position": {
"lat": 48.80865,
"lng": 2.26594
},
"access": [
{
"lat": 48.8087,
"lng": 2.2659
}
],
"distance": 5558,
"categories": [
{
"id": "200-2200-0000",
"name": "Théâtre, musique et culture",
"primary": true
},
{
"id": "200-2000-0015",
"name": "Salle de spectacle/concert"
},
{
"id": "550-5520-0000",
"name": "Aire de loisirs"
}
],
"references": [
{
"supplier": {
"id": "yelp"
},
"id": "xJtl5krT8OZ3QYXtMl6Leg"
}
],
"highlights": {
"title": [
{
"start": 7,
"end": 12
}
],
"address": {
"label": [
{
"start": 7,
"end": 12
}
]
}
}
}
],
"queryTerms": [
]
}

Get the complete graph information (properties,id etc ) in hierarchical way under a particular node in gremlin

I am able to get all the child nodes and its related information under a parent node but in a array of objects format , for the query
g.V(4128).repeat(out()).emit()
[
{
"id": 4152.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1l3-37c-1l1",
"value": "XYZ"
}
],
"description": [
{
"id": "1zb-37c-7wl",
"value": "XYZ World"
}
],
"shortName": [
{
"id": "16v-37c-745",
"value": "XYZ"
}
]
}
},
{
"id": 4176.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1l6-380-1l1",
"value": "XYZ-XYZW"
}
],
"description": [
{
"id": "1ze-380-7wl",
"value": "XYZ West Campus"
}
],
"shortName": [
{
"id": "16y-380-745",
"value": "XYZW"
}
]
}
},
{
"id": 8344.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3kj-6fs-1l1",
"value": "XYZ-XYZE"
}
],
"description": [
{
"id": "3yr-6fs-7wl",
"value": "XYZ East Campus"
}
],
"shortName": [
{
"id": "36b-6fs-745",
"value": "XYZE"
}
]
}
},
{
"id": 4104.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1kx-360-1l1",
"value": "XYZ-XYZW-P1"
}
],
"description": [
{
"id": "1z5-360-7wl",
"value": "XYZ West Campus-Phase-1"
}
],
"shortName": [
{
"id": "16p-360-745",
"value": "P1"
}
]
}
},
{
"id": 4296.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "1ll-3bc-1l1",
"value": "XYZ-XYZW-P3"
}
],
"description": [
{
"id": "1zt-3bc-7wl",
"value": "XYZ West Campus-Phase-3"
}
],
"shortName": [
{
"id": "17d-3bc-745",
"value": "P3"
}
]
}
},
{
"id": 8200.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3k1-6bs-1l1",
"value": "XYZ-XYZW-P2"
}
],
"description": [
{
"id": "3y9-6bs-7wl",
"value": "XYZ West Campus-Phase-2"
}
],
"shortName": [
{
"id": "35t-6bs-745",
"value": "P2"
}
]
}
},
{
"id": 8224.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3yc-6cg-1l1",
"value": "XYZ-XYZE-P1"
}
],
"description": [
{
"id": "4ck-6cg-7wl",
"value": "XYZ East Campus-Phase-1"
}
],
"shortName": [
{
"id": "3k4-6cg-745",
"value": "P1"
}
]
}
},
{
"id": 8392.0,
"label": "location",
"type": "vertex",
"properties": {
"displayName": [
{
"id": "3kp-6h4-1l1",
"value": "XYZ-XYZE-P2"
}
],
"description": [
{
"id": "3yx-6h4-7wl",
"value": "XYZ East Campus-Phase-2"
}
],
"shortName": [
{
"id": "36h-6h4-745",
"value": "P2"
}
]
}
}
]
I need to get the same information in hierarchial way .
I tried the following query
g.V(4128).repeat(out()).emit().tree().by(__.valueMap(true))
but I don't get the valid json.
I need to get the above data but in hierarchical way
Updated
I tried the following query
g.V(4128).repeat(out()).emit().tree().by(__.valueMap(true))
And the response I received was
[
{
"{label = org, name = [XYZ], orgId = [00000000-0000-0000-0000-000000000001], desc = [XYZ Organization], id = 4128}": {
"{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 8272}": {
},
"{label = location, displayName = [XYZ], description = [XYZ World], shortName = [XYZ], id = 4152}": {
"{label = location, displayName = [XYZ-XYZW], description = [XYZ West Campus], shortName = [XYZW], id = 4176}": {
"{label = location, displayName = [XYZ-XYZW-P3], description = [XYZ West Campus-Phase-3], shortName = [P3], id = 4296}": {
},
"{label = location, displayName = [XYZ-XYZW-P2], description = [XYZ West Campus-Phase-2], shortName = [P2], id = 8200}": {
},
"{label = location, displayName = [XYZ-XYZW-P1], description = [XYZ West Campus-Phase-1], shortName = [P1], id = 4104}": {
}
},
"{label = location, displayName = [XYZ-XYZE], description = [XYZ East Campus], shortName = [XYZE], id = 8344}": {
"{label = location, displayName = [XYZ-XYZE-P1], description = [XYZ East Campus-Phase-1], shortName = [P1], id = 8224}": {
},
"{label = location, displayName = [XYZ-XYZE-P2], description = [XYZ East Campus-Phase-2], shortName = [P2], id = 8392}": {
}
}
},
"{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 8248}": {
},
"{label = location, displayName = [hh], description = [hfds], shortName = [kk], id = 12488}": {
}
}
}
]
Now as you can see the json so received is not appropriate because the key contains fields that are '=' separated .
Now as for the answer I also tried the folowing query
g.V(4128).repeat(out()).emit().tree().next()
but the response returned only ids and didnt resolve the vertex properties
[
{
"4128": {
"8272": {},
"4152": {
"4176": {
"4104": {},
"4296": {},
"8200": {}
},
"8344": {
"8224": {},
"8392": {}
}
},
"8248": {},
"12488": {}
}
}
]
For the context I am using the following configurations in my gremlin-server.yaml file
authentication: {className: org.apache.tinkerpop.gremlin.server.auth.AllowAllAuthenticator,
config: null}
channelizer: org.apache.tinkerpop.gremlin.server.channel.HttpChannelizer
graphs: {graph: 'D:/graph-data-access/src/test/resources/titan-inmemory.properties'}
gremlinPool: 8
host: localhost
maxAccumulationBufferComponents: 1024
maxChunkSize: 8192
maxContentLength: 65536
maxHeaderSize: 8192
maxInitialLineLength: 4096
metrics:
consoleReporter: null
csvReporter: null
gangliaReporter: null
graphiteReporter: null
jmxReporter: null
slf4jReporter: {enabled: true, interval: 180000, loggerName: org.apache.tinkerpop.gremlin.server.Settings$Slf4jReporterMetrics}
plugins: [aurelius.titan, tinkerpop.gephi]
port: 8182
processors: []
resultIterationBatchSize: 64
scriptEngines:
gremlin-groovy:
config: null
imports: [java.lang.Math]
scripts: ['D:/graph-data-access/src/test/resources/generate-locations.groovy']
staticImports: [java.lang.Math.PI]
scriptEvaluationTimeout: 30000
serializedResponseTimeout: 30000
serializers:
- className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0
config: {useMapperFromGraph: graph}
- className: org.apache.tinkerpop.gremlin.driver.ser.GryoMessageSerializerV1d0
config: {serializeResultToString: true}
- className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerV1d0
config: {useMapperFromGraph: graph}
- className: org.apache.tinkerpop.gremlin.driver.ser.GraphSONMessageSerializerGremlinV1d0
config: {useMapperFromGraph: graph}
ssl: {enabled: false, keyCertChainFile: null, keyFile: null, keyPassword: null, trustCertChainFile: null}
threadPoolBoss: 1
threadPoolWorker: 1
writeBufferHighWaterMark: 65536
writeBufferLowWaterMark: 32768
I am using Tinkerpop version 3.0.1 incubating as I am trying to use Titan 1.0.0 with DynamoDb as storage backend for the remote connection.
Not sure what you mean by "but i don't get valid json". You should be able to serialized a Tree in GraphSON 1.0 if you don't include by(valueMap(true)) and just serialize the full vertex. There is a failure if you try to do it as just maps as it makes some assumptions about there being a graph Element as the key (which is probably bad).
gremlin> mapper = graph.io(graphson()).mapper().version(GraphSONVersion.V1_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper#6b2e0f78
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().next())
==>{"1":{"key":{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},"value":{"2":{"key":{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},"value":{}},"3":{"key":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"value":{}},"4":{"key":{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},"value":{"3":{"key":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"value":{}},"5":{"key":{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},"value":{}}}}}}}
That formats to:
{
"1": {
"key": {
"id": 1,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 0,
"value": "marko"
}],
"age": [{
"id": 2,
"value": 29
}]
}
},
"value": {
"2": {
"key": {
"id": 2,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 3,
"value": "vadas"
}],
"age": [{
"id": 4,
"value": 27
}]
}
},
"value": {}
},
"3": {
"key": {
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
},
"value": {}
},
"4": {
"key": {
"id": 4,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 7,
"value": "josh"
}],
"age": [{
"id": 8,
"value": 32
}]
}
},
"value": {
"3": {
"key": {
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
},
"value": {}
},
"5": {
"key": {
"id": 5,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 9,
"value": "ripple"
}],
"lang": [{
"id": 10,
"value": "java"
}]
}
},
"value": {}
}
}
}
}
}
}
In GraphSON 2.0 you get this:
gremlin> mapper = graph.io(graphson()).mapper().version(GraphSONVersion.V2_0).create().createMapper()
==>org.apache.tinkerpop.shaded.jackson.databind.ObjectMapper#3ffb3598
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().next())
==>{"#type":"g:Tree","#value":[{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":1},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":0},"value":"marko","label":"name"}}],"age":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":2},"value":{"#type":"g:Int32","#value":29},"label":"age"}}]}}},"value":{"#type":"g:Tree","#value":[{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":2},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":3},"value":"vadas","label":"name"}}],"age":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":4},"value":{"#type":"g:Int32","#value":27},"label":"age"}}]}}},"value":{"#type":"g:Tree","#value":[]}},{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":3},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":5},"value":"lop","label":"name"}}],"lang":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":6},"value":"java","label":"lang"}}]}}},"value":{"#type":"g:Tree","#value":[]}},{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":4},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":7},"value":"josh","label":"name"}}],"age":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":8},"value":{"#type":"g:Int32","#value":32},"label":"age"}}]}}},"value":{"#type":"g:Tree","#value":[{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":3},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":5},"value":"lop","label":"name"}}],"lang":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":6},"value":"java","label":"lang"}}]}}},"value":{"#type":"g:Tree","#value":[]}},{"key":{"#type":"g:Vertex","#value":{"id":{"#type":"g:Int32","#value":5},"label":"vertex","properties":{"name":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":9},"value":"ripple","label":"name"}}],"lang":[{"#type":"g:VertexProperty","#value":{"id":{"#type":"g:Int32","#value":10},"value":"java","label":"lang"}}]}}},"value":{"#type":"g:Tree","#value":[]}}]}}]}}]}
which formats as:
{
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["marko"],
"id": {
"#type": "g:Int32",
"#value": 1
},
"age": [{
"#type": "g:Int32",
"#value": 29
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["vadas"],
"id": {
"#type": "g:Int32",
"#value": 2
},
"age": [{
"#type": "g:Int32",
"#value": 27
}]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["josh"],
"id": {
"#type": "g:Int32",
"#value": 4
},
"age": [{
"#type": "g:Int32",
"#value": 32
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["ripple"],
"id": {
"#type": "g:Int32",
"#value": 5
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}]
}
}]
}
}]
}
Note that you can also use by(valueMap(true)) in the case of GraphSON 2.0:
gremlin> mapper.writeValueAsString(g.V(1).repeat(out()).emit().tree().by(valueMap(true)).next())
==>{"#type":"g:Tree","#value":[{"key":{"label":"vertex","name":["marko"],"id":{"#type":"g:Int32","#value":1},"age":[{"#type":"g:Int32","#value":29}]},"value":{"#type":"g:Tree","#value":[{"key":{"label":"vertex","name":["lop"],"id":{"#type":"g:Int32","#value":3},"lang":["java"]},"value":{"#type":"g:Tree","#value":[]}},{"key":{"label":"vertex","name":["vadas"],"id":{"#type":"g:Int32","#value":2},"age":[{"#type":"g:Int32","#value":27}]},"value":{"#type":"g:Tree","#value":[]}},{"key":{"label":"vertex","name":["josh"],"id":{"#type":"g:Int32","#value":4},"age":[{"#type":"g:Int32","#value":32}]},"value":{"#type":"g:Tree","#value":[{"key":{"label":"vertex","name":["ripple"],"id":{"#type":"g:Int32","#value":5},"lang":["java"]},"value":{"#type":"g:Tree","#value":[]}},{"key":{"label":"vertex","name":["lop"],"id":{"#type":"g:Int32","#value":3},"lang":["java"]},"value":{"#type":"g:Tree","#value":[]}}]}}]}}]}
which formats to:
{
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["marko"],
"id": {
"#type": "g:Int32",
"#value": 1
},
"age": [{
"#type": "g:Int32",
"#value": 29
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["vadas"],
"id": {
"#type": "g:Int32",
"#value": 2
},
"age": [{
"#type": "g:Int32",
"#value": 27
}]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["josh"],
"id": {
"#type": "g:Int32",
"#value": 4
},
"age": [{
"#type": "g:Int32",
"#value": 32
}]
},
"value": {
"#type": "g:Tree",
"#value": [{
"key": {
"label": "vertex",
"name": ["ripple"],
"id": {
"#type": "g:Int32",
"#value": 5
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}, {
"key": {
"label": "vertex",
"name": ["lop"],
"id": {
"#type": "g:Int32",
"#value": 3
},
"lang": ["java"]
},
"value": {
"#type": "g:Tree",
"#value": []
}
}]
}
}]
}
}]
}
If you are on older version of TinkerPop going back to 3.0.x, then there are some limitations with tree that aren't easily worked around. The only approach is to take a raw tree and post-process it to get it to a form that is friendly to JSON. Here is one way to do that:
gremlin> convert = { it.collectEntries{ k,v -> [(k.id()): [k, v.isEmpty() ? v : convert(v)]] }}
==>groovysh_evaluate$_run_closure1#255e5e2e
gremlin> t = g.V(1).repeat(out()).emit().tree().next()
==>v[1]={v[2]={}, v[3]={}, v[4]={v[3]={}, v[5]={}}}
gremlin> convert(t)
==>1=[v[1], {2=[v[2], {}], 3=[v[3], {}], 4=[v[4], {3=[v[3], {}], 5=[v[5], {}]}]}]
gremlin> mapper.writeValueAsString(convert(t))
==>{"1":[{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},{"2":[{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},{}],"3":[{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},{}],"4":[{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},{"3":[{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},{}],"5":[{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},{}]}]}]}
This formats in GraphSON 1.0 (which is what is used in 3.0.x) to:
{
"1": [{
"id": 1,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 0,
"value": "marko"
}],
"age": [{
"id": 2,
"value": 29
}]
}
}, {
"2": [{
"id": 2,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 3,
"value": "vadas"
}],
"age": [{
"id": 4,
"value": 27
}]
}
}, {}],
"3": [{
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
}, {}],
"4": [{
"id": 4,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 7,
"value": "josh"
}],
"age": [{
"id": 8,
"value": 32
}]
}
}, {
"3": [{
"id": 3,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 5,
"value": "lop"
}],
"lang": [{
"id": 6,
"value": "java"
}]
}
}, {}],
"5": [{
"id": 5,
"label": "vertex",
"type": "vertex",
"properties": {
"name": [{
"id": 9,
"value": "ripple"
}],
"lang": [{
"id": 10,
"value": "java"
}]
}
}, {}]
}]
}]
}
The convert() function i created just recursively iterates the tree and turns every [vertex: children] into [vertexId: [vertex,children]. You can convert to whatever format you like, so long as you build valid JSON where there is a usable String key field. I could have just as easily made the convert() function return [vertexId: [node: vertex, leaves: children]:
gremlin> convert = { it.collectEntries{ k,v -> [(k.id()): [node: k, leaves:v.isEmpty() ? v : convert(v)]] }}
==>groovysh_evaluate$_run_closure1#2ab5afc7
gremlin> mapper.writeValueAsString(convert(t))
==>{"1":{"node":{"id":1,"label":"vertex","type":"vertex","properties":{"name":[{"id":0,"value":"marko"}],"age":[{"id":2,"value":29}]}},"leaves":{"2":{"node":{"id":2,"label":"vertex","type":"vertex","properties":{"name":[{"id":3,"value":"vadas"}],"age":[{"id":4,"value":27}]}},"leaves":{}},"3":{"node":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"leaves":{}},"4":{"node":{"id":4,"label":"vertex","type":"vertex","properties":{"name":[{"id":7,"value":"josh"}],"age":[{"id":8,"value":32}]}},"leaves":{"3":{"node":{"id":3,"label":"vertex","type":"vertex","properties":{"name":[{"id":5,"value":"lop"}],"lang":[{"id":6,"value":"java"}]}},"leaves":{}},"5":{"node":{"id":5,"label":"vertex","type":"vertex","properties":{"name":[{"id":9,"value":"ripple"}],"lang":[{"id":10,"value":"java"}]}},"leaves":{}}}}}}}

Resources