Return a nested data structure from a SPARQL query

前端 未结 3 850
不知归路
不知归路 2021-02-15 16:22

If I have a graph with this kind of structure:

@prefix  :        .
@prefix  rdf:     .
         


        
3条回答
  •  心在旅途
    2021-02-15 17:10

    You could use a CONSTRUCT query with JSON-LD Framing.

    Example query (on DBpedia endpoint)

    CONSTRUCT
    {
      ?person  rdf:type       foaf:Person ;
               dbo:birthName  ?name1s ;
               dbo:birthDate  ?date1s ;
               dbo:spouse     ?spouse .
      ?spouse  rdf:type       foaf:Person ; 
               dbo:birthName  ?name2s ;
               dbo:birthDate  ?date2s .
    }
    WHERE
    {
      ?person  dbo:birthName  ?name1 ;
               dbo:birthDate  ?date1 ;
               dbo:spouse     ?spouse .
      ?spouse  dbo:birthName  ?name2 ;
               dbo:birthDate  ?date2 .
      BIND (str(?name1) AS ?name1s)
      BIND (str(?date1) AS ?date1s)
      BIND (str(?name2) AS ?name2s)
      BIND (str(?date2) AS ?date2s)
      VALUES (?person) { ( dbr:Brad_Pitt ) }
    }
    

    Output (in JSON-LD format with context)

    { "@context": {
        "spouse": { "@id": "http://dbpedia.org/ontology/spouse"},
        "birthDate": { "@id": "http://dbpedia.org/ontology/birthDate" },
        "birthName": { "@id": "http://dbpedia.org/ontology/birthName" } },
      "@graph": [
        { "@id": "http://dbpedia.org/resource/Angelina_Jolie",
          "birthName": "Angelina Jolie Voight",
          "birthDate": "1975-06-04" },
        { "@id": "http://dbpedia.org/resource/Brad_Pitt",
          "@type": "http://xmlns.com/foaf/0.1/Person",
          "birthName": "William Bradley Pitt",
          "spouse": [ "http://dbpedia.org/resource/Angelina_Jolie",
                      "http://dbpedia.org/resource/Jennifer_Aniston" ],
          "birthDate": "1963-12-18" },
        { "@id": "http://dbpedia.org/resource/Jennifer_Aniston",
          "birthName": "Jennifer Joanna Aniston",
          "birthDate": "1969-02-11" }
    ] }
    

    JSON-LD Frame (very simple)

    {
      "@context": {"dbo": "http://dbpedia.org/ontology/",
                   "dbr": "http://dbpedia.org/resource/",
                   "foaf": "http://xmlns.com/foaf/0.1/"},
      "dbo:spouse": {
       }
    }
    

    Framed JSON-LD (playground)

    {
      "@context": {
        "dbo": "http://dbpedia.org/ontology/",
        "dbr": "http://dbpedia.org/resource/",
        "foaf": "http://xmlns.com/foaf/0.1/"
      },
      "@graph": [
        {
          "@id": "dbr:Brad_Pitt",
          "@type": "foaf:Person",
          "dbo:birthDate": "1963-12-18",
          "dbo:birthName": "William Bradley Pitt",
          "dbo:spouse": [
            {
              "@id": "dbr:Angelina_Jolie",
              "@type": "foaf:Person",
              "dbo:birthDate": "1975-06-04",
              "dbo:birthName": "Angelina Jolie Voight"
            },
            {
              "@id": "dbr:Jennifer_Aniston",
              "@type": "foaf:Person",
              "dbo:birthDate": "1969-02-11",
              "dbo:birthName": "Jennifer Joanna Aniston"
            }
          ]
        }
      ]
    }
    

    Some discussion

    JSON-LD Framing is an unofficial, yet well implemented specification that describes a deterministic layout for serializing an RDF graph into a particular JSON-LD document layout.

    Obviously, with blank nodes property lists, one can achieve something structurally similar to the output you want:

    Brad_Pitt
            dbo:birthName   "William Bradley Pitt" ;
            dbo:birthDate   "1963-12-18" .
            dbo:spouse  [   dbo:birthName   "Angelina Jolie Voight" ;
                            dbo:birthDate   "1975-06-04" ] ,
                        [   dbo:birthName   "Jennifer Joanna Aniston" ;
                            dbo:birthDate   "1969-02-11" ] .
    

    However, this is Turtle, not JSON, and nobody can garantee that these blank nodes property lists will be used in serialization.

提交回复
热议问题