json-ld / json-ld.org

JSON for Linked Data's documentation and playground site
https://json-ld.org/
Other
858 stars 152 forks source link

JSON-LD Playground doesn't parse correctly Named Graphs #756

Closed Javingka closed 3 years ago

Javingka commented 3 years ago

Hi guys, here is the thing:

If you take the following JSON-LD from the Documentation

{
  "@context": {
    "@version": 1.1,
    "generatedAt": {
      "@id": "http://www.w3.org/ns/prov#generatedAtTime",
      "@type": "http://www.w3.org/2001/XMLSchema#date"
    },
    "Person": "http://xmlns.com/foaf/0.1/Person",
    "name": "http://xmlns.com/foaf/0.1/name",
    "knows": "http://xmlns.com/foaf/0.1/knows",
    "claim": {
     "@id": "https://w3id.org/credentials#claim",
      "@container": "@graph"
    }
  },
  "generatedAt": "2012-04-09",
  "claim": [
    {
      "@id": "http://manu.sporny.org/about#manu",
      "@type": "Person",
      "name": "Manu Sporny",
      "knows": "http://greggkellogg.net/foaf#me"
    }, {
      "@id": "http://greggkellogg.net/foaf#me",
      "@type": "Person",
      "name": "Gregg Kellogg",
      "knows": "http://manu.sporny.org/about#manu"
    }
  ]
}

Paste it into the Playground and parse it into the N-Quads, what I do expect to see is the NamedGraph actually named to _:claim the way the Doc explains, but instead you see anonymous graphs :b1 and :b2 like this:

<http://greggkellogg.net/foaf#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> _:b2 .
<http://greggkellogg.net/foaf#me> <http://xmlns.com/foaf/0.1/knows> "http://manu.sporny.org/about#manu" _:b2 .
<http://greggkellogg.net/foaf#me> <http://xmlns.com/foaf/0.1/name> "Gregg Kellogg" _:b2 .
<http://manu.sporny.org/about#manu> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> _:b1 .
<http://manu.sporny.org/about#manu> <http://xmlns.com/foaf/0.1/knows> "http://greggkellogg.net/foaf#me" _:b1 .
<http://manu.sporny.org/about#manu> <http://xmlns.com/foaf/0.1/name> "Manu Sporny" _:b1 .
_:b0 <http://www.w3.org/ns/prov#generatedAtTime> "2012-04-09"^^<http://www.w3.org/2001/XMLSchema#date> .
_:b0 <https://w3id.org/credentials#claim> _:b1 .
_:b0 <https://w3id.org/credentials#claim> _:b2 .

Is that right and I'm lost about something, or it is a parsing bug?

Thanks.

gkellogg commented 3 years ago

In RDF, the identifier of a blank node conveys no meaning, and the JSON-LD algorithms normatively rename blank nodes, to prevent collisions. The semantics are the same, in-spite of the renaming of the blank node identifiers.

You'll find that blank node identifiers are renamed as part of the Flattening algorithm.

Javingka commented 3 years ago

Hi! Just leaving here a way I found to actually keep the Graphs Names when parsing to for example N-Quads.

I took off the claim property from within the @context and leave the graph definition on the JSON root level, as a blank node _:b0 holding an object with two properties @id and @graph, like this:

{
    "@context": {
    "@version": 1.1,
    "generatedAt": {
        "@id": "http://www.w3.org/ns/prov#generatedAtTime",
        "@type": "http://www.w3.org/2001/XMLSchema#date"
    },
    "Person": "http://xmlns.com/foaf/0.1/Person",
    "name": "http://xmlns.com/foaf/0.1/name",
    "knows": "http://xmlns.com/foaf/0.1/knows"
    },
    "generatedAt": "2012-04-09",
    "_:b0":{
        "@id": "https://w3id.org/credentials#claim",
        "@graph":[
            {
                  "@id": "http://manu.sporny.org/about#manu",
                  "@type": "Person",
                  "name": "Manu Sporny",
                  "knows": "http://greggkellogg.net/foaf#me"
            }, {
                  "@id": "http://greggkellogg.net/foaf#me",
                  "@type": "Person",
                  "name": "Gregg Kellogg",
                  "knows": "http://manu.sporny.org/about#manu"
            }
          ]
    }
}

doing so I got it right, nquad parse result:

<http://greggkellogg.net/foaf#me> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <https://w3id.org/credentials#claim> .
<http://greggkellogg.net/foaf#me> <http://xmlns.com/foaf/0.1/knows> "http://manu.sporny.org/about#manu" <https://w3id.org/credentials#claim> .
<http://greggkellogg.net/foaf#me> <http://xmlns.com/foaf/0.1/name> "Gregg Kellogg" <https://w3id.org/credentials#claim> .
<http://manu.sporny.org/about#manu> <http://www.w3.org/1999/02/22-rdf-syntax-ns#type> <http://xmlns.com/foaf/0.1/Person> <https://w3id.org/credentials#claim> .
<http://manu.sporny.org/about#manu> <http://xmlns.com/foaf/0.1/knows> "http://greggkellogg.net/foaf#me" <https://w3id.org/credentials#claim> .
<http://manu.sporny.org/about#manu> <http://xmlns.com/foaf/0.1/name> "Manu Sporny" <https://w3id.org/credentials#claim> .
_:b0 <http://www.w3.org/ns/prov#generatedAtTime> "2012-04-09"^^<http://www.w3.org/2001/XMLSchema#date> .

FYI @pchampin @gkellogg

gkellogg commented 3 years ago

It is simply re-generating the same blank node identifiers. Blank node identifiers are issued in sequence, starting from "_:b0", based on the sorted order from flatting. If you create a document using the same ordering, it will be reproduced.