Query loaded jsonld, wrong output

I loaded many complex jsonld documents into virtuoso and I experiment with it with a lot of success. However, I have problem querying some part of the jsonld. Here I used simple jsonld (I show part of it) to simplify the issue.

{
.
.
"exams": [
				{
					"@type": "Exams",
					"@id": "exam1",
					"subject": "Physics",
					"result": 77
					},
					{
					"@type": "Exams",
					"@id": "exam2",
					"subject": "Chemistry",
					"result": 81
					}
	]
					
.
.
}

I used the following SparQl query to get the “subject” with its corresponding “result”

prefix study:<https://study.org/>
SELECT  ?subject ?result
  {
   study:exams/study:subject ?subject;
   study:exams/study:result ?result.
  }

here is the query output I got which is not fully correct:

subject	       result
"Physics"	     77
"Physics"        81
"Chemistry"      77
"Chemistry"      81

The correct output should be:

subject	         result
"Physics"		   77
"Chemistry"        81

Your query does not appear valid as it gives error Virtuoso 37000 Error SP030: SPARQL compiler, line 7: syntax error at '/' before 'study:subject' when attempting to run ?

Anyway, have you tried adding the distinct to the select list ie SELECT distinct ?subject ?result to make the results distinct ?

@starz10de

You probably want to:

prefix study:<https://study.org/> 
SELECT  ?subject ?result   { 
 []   study:exams [ study:subject ?subject;    study:result ?result ]   
}

HTH

1 Like

Thanks a lot ! it works fine

just another question, I would like to retrieve the "@type" and "@id" as well, however, it seems I can’t just treat them as standard data property, e.g.,:

study:exams [ study:subject ?subject; study:id ?ID; study:type ?Type ; study:result ?result ]

The final result should look like:

subject	          ID            Type        result
"Physics"		  exam1         Exams         77
"Chemistry"       exam2         Exams         81

any suggestion?

The ‘@id’ and ‘@type’ cannot be a NCName, these are IRIs.
you should do:

prefix study: <https://study.org/>  
SELECT ?id ?type ?subject ?result   
{   []   study:exams ?id . 
    ?id  study:subject ?subject;    
    study:result ?result ; rdf:type ?type   .    
} 

you will get IRIs for subject and for type of entities where IRIs depends of JSON-LD context.

HTH

1 Like

thanks a lot for your kind support
Last question how to retrieve the id of e.g., subject which is https://study.org/subject

here the context:

"@context": {
		"@version": 1.1,
		"@base": ". . .",
		"@vocab": "https://study.org/",
.
.

@starz10de

The @base can’t be ‘…’ it should be URI.
Also did you tried query above, result for ?id ?

HTH

Yes, I just put . . . for presentation purpose. The above query can retrieve "@id": "exam1", and "@id": "exam2", as they are exist under [exams], the other Ids should be retrieved based on the context/vocab.
I can query the subject id using:

SELECT ?s ?p ?o WHERE { ?s ?p ?o . FILTER( (?p= <https://study.org/subject> )) }

but I would like to get it automatically using:

prefix study: <https://study.org/>  
SELECT ?id ?type ?subject ?result   
{   []   study:exams ?id . 
    ?id  study:subject ?subject;    
    study:result ?result ; rdf:type ?type   .    
}