'JSON-LD framing specify ordering of rdfs

@prefix emp: <http://example.com/employee/> .
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
@prefix rdfs: <http://www.w3.org/2000/01/rdf-schema#> .

emp:hasName rdf:type rdfs:Property .

emp:dateOfBirth  rdf:type rdfs:Property ;
    rdf:after emp:passportNumber .

emp:passportNumber rdf:type rdfs:Property .

Given the above Turtle RDF model, I want to generate the following JSON-LD:

"@graph": [
    {
        "@id": "emp:hasName",
        "@type": "rdfs:Property"
    },
    {
        "@id": "emp:passportNumber",
        "@type": "rdfs:Property"
    },
    {
        "@id": "emp:dateOfBirth",
        "@type": "rdfs:Property",
    }
]

Notice that emp:dateOfBirth is ordered after emp:passportNumber in the list as I specified rdf:after "emp:passportNumber" in the model.

How could I accomplish this via JSON-LD Framing?

Is it safe to rely on the order in which these rdfs are declared in, as most RDF->JSON-LD libs seem to preserve the ordering (despite the specs defining them as unordered)?

I do not want to explicitly define an exhaustive ordered list in the RDF model as the list would be huge and only a few RDFs need to be ordered (eg. I do not care where emp:hasName is).



Solution 1:[1]

The JSON-LD algorithms will optionally sort keys in an object, not not sort objects themselves. One way you might be able to do this is using an id map, so that the subject IRIs of the different RDFS terms were then used as the keys of this map. For that, they would all need to be a value of some other entity. One trick for doing this is to use rdfs:isDefinedBy as a reverse property, so that the ontology itself appears as the top-level object with reverse relationships to the terms it defines. Combining a reverse property with an id map would make the object value have subject IRIs as keys, which could then be ordered.

Solution 2:[2]

The fact that you see an "order" of the triples in JSON-LD is just an artifact of the serialization, something you would also see in RDF/XML (as XML elements are ordered) and Turtle (as text files are ordered), but it is not something to rely on to be stable. An implementation can very easily use a hash set to store the triples before converting to another format, and it could rearrange the triples as it sees fit.

Now JSON-LD Framing seems to be used to produce compacted JSON documents, not flattened (i.e. with @graph), but if it can be used both ways, I suppose this should work:

{
  "@context": ...,
  "@graph": [
    {
      "@id": "emp:passportNumber"
    },
    {
      "@id": "emp:dateOfBirth"
    }
  ]
}

Assuming you are fine with having a single global order. There is however nothing in that specification to give you the ability to indicate the order in RDF itself.

If you need this for the sake of presentation, I suggest you preserve the ordering relation in the RDF itself for as long as possible. As there doesn't seem to be an rdf:after, you could use something like xhv:prev for that general purpose, or use something more specific, depending on the context. You might also use SPARQL to order the data based on that property, as SPARQL results are ordered.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gregg Kellogg
Solution 2 IS4