4

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!

2
  • perhaps you can use this library -> github.com/digitalbazaar/jsonld.js the jsonld.compact() function seems to be what you are looking for ... Commented Nov 7, 2015 at 13:50
  • Thanks a lot David !! That's exactly the function I needed, I find jsonld.js quite useful. My doubt was about how to generate the context that the jsonld.compact() needs as an input in this particular case. Commented Nov 7, 2015 at 18:46

1 Answer 1

7

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"
}
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks a lot @markus-lanthaler !! Somehow I missed keyword aliasing.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.