Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JSON to JSON LD with minimal changes to the original JSON (all changes in JSON-LD context)

Tags:

json

rdf

json-ld

TL;DR

If I have a JSON document like

{
  "policyid": "http://example.com/policy:0099",
  "policytype": "http://www.w3.org/ns/odrl/2/Set" 
}

and I want have a JSON-LD document similar to

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "policytype": { "@id": "rdf:type",  "@type": "@id" }
   }
   "@id" : "http://example.com/policy:0099",
   "policytype": "http://www.w3.org/ns/odrl/2/Set"
}

Is it possible to not change the name/vale pair { "policyid": "http://example.com/policy:0099" } to { "@id" : "http://example.com/policy:0099" } but rather say something in the context to say "policyid" -> "@id".

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "policytype": { "@id": "rdf:type",  "@type": "@id" },
    #### something here that says "policyid" -> "@id"
   }
   "policyid" : "http://example.com/policy:0099",
   "policytype": "http://www.w3.org/ns/odrl/2/Set"
}

I was going through the spec example and couldn't find how to do that.

More context

Say if we have a model which has specification for RDF, and JSON Encoding, for example, ODRL 2.1 Ontology and ODRL Version 2.1 JSON Encoding.

I want to start from JSON and generate JSON-LD by mapping JSON Encoding to the ODRL ontology.

{
  "policyid": "http://example.com/policy:0099",
  "policytype": "http://www.w3.org/ns/odrl/2/Set",  
  "permissions": [{
     "target": "http://example.com/asset:9898",
     "action": "http://www.w3.org/ns/odrl/2/reproduce"
  }]
}

The following is RDF model I want to convert this json to. (I will put the Turtle serialization to make it more readable).

@prefix odrl: <http://www.w3.org/ns/odrl/2/> .

<http://example.com/policy:0099> a odrl:Set .
<http://example.com/policy:0099> odrl:permission _:perm0 .
_:perm0 odrl:action <http://www.w3.org/ns/odrl/2/reproduce> .
_:perm0 odrl:target <http://example.com/asset:9898> . 

I can do this with almost with minimal changes with a context as the following.

{
  "@context": { 
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "odrl": "http://www.w3.org/ns/odrl/2/",
    "policytype": { "@id": "rdf:type",  "@type": "@id" },
    "permissions": { "@id": "odrl:permission",  "@type": "@id"},
     "target" : {"@id": "odrl:target",  "@type": "@id" },
     "action" : {"@id": "odrl:action",  "@type": "@id" }
  },
  "@id": "http://example.com/policy:0099",
  "policytype": "http://www.w3.org/ns/odrl/2/Set",
  "permissions": [{ 
       "target": "http://example.com/asset:9898",
       "action": "http://www.w3.org/ns/odrl/2/reproduce" }]
}

But if I want to keep the original JSON as it is, is there a way to say "policyid" -> "@id" in the context?

Many thanks!

like image 951
Nandana Avatar asked Mar 15 '23 05:03

Nandana


1 Answers

Is it possible to not change the name/vale pair { "policyid": "http://example.com/policy:0099" } to { "@id" : "http://example.com/policy:0099" } but rather say something in the context to say "policyid" -> "@id".

Yes, you simply map policyid to @id. This is called keyword aliasing in the spec. So your example would look like this:

{
  "@context": {
    "rdf": "http://www.w3.org/1999/02/22-rdf-syntax-ns#",
    "policyid": "@id",
    "policytype": { "@id": "rdf:type",  "@type": "@id" }
   },
   "policyid" : "http://example.com/policy:0099",
   "policytype": "http://www.w3.org/ns/odrl/2/Set"
}
like image 182
Markus Lanthaler Avatar answered May 01 '23 21:05

Markus Lanthaler