Search code examples
rdfjson-ld

How can I write a cyclic RDF graph in JSON-LD?


My graph is the follow:

  [Natasha]--- knows--> [Bob]
  [Bob]--- brother of--> [Alice]
  [Alice]--- play with--> [Natasha]

How can I embed it in JSON-LD?

My current solution that doesn't work:

{
"@context" : {
   "Natasha" : "http://names.example.org/resource/name/Natasha",
   "Bob" : "http://names.example.org/resource/name/Bob",
   "Alice" : "http://names.example.org/resource/name/Alice",
   "knows" : "http://example.com/knows",
   "brother-of" : "http://example.com/brother-of",
   "play-with" : "http://example.com/play-with"
},
 "@id" : "Natasha", 
  "knows" : 
        {  "@id" : "Bob", 
           "brother-of" : { 
                    "@id" : "Alice",
              "play-with" : "Natasha"
        }

        }
    }

Solution

  • This value gets interpreted as plain text, not as URI:

    "play-with" : "Natasha"
    

    To get it interpreted as URI, you can either define it in your context:

    "@context": {
      "play-with": {
        "@id": "http://example.com/play-with", 
        "@type": "@id"
      }
    }
    

    Or you can make it explicit when providing a value (like you already do for brother-of):

    "play-with" : {"@id": "Natasha"}