Here is a data object for a user with ID 123, that is age 50 and likes spaghetti.
[ a :user;
:user_id 123;
:demographics [ :age 50 ];
:favorite_food [ :italian [ :spaghetti true ]]
].
Is there any sparql query where I could get back all the triples related to this user without having to do a UNION for each possible blank node value follow?
I'm trying to convert json into triples, then turn the triples back into json, so it would be very convenient be able to query of objects of unknown nesting depth without adding a parent ID property to every child element of the json.
This seems to work but I would have to do a UNION for every layer of nesting
select ?e1 ?a1 ?v1 { ?e :user_id 123.
{ ?e ?a1 ?v1. BIND(?e AS ?e1) }
UNION { ?e ?a ?v. ?v ?a1 ?v1. BIND(?v AS ?e1). } }
UninformedUser has a pointed out a solution. From the look of the query it seems like the recursive property path trick wouldn't grab the first level before nesting, but it does grab all data.
select ?s ?p ?o { ?e :user_id 123. ?e (<>|!<>)* ?s. ?s ?p ?o }
@UninformedUser provided a solution in a comment. Though the OP thought this query using the recursive property path trick wouldn't grab the first level before nesting, it does grab all their desired data.
SELECT ?s ?p ?o
{ ?e :user_id 123 .
?e (<>|!<>)* ?s .
?s ?p ?o
}