How to specify 1 to many and many to 1 relations in json-ld document?

422 views Asked by At

How can I specify one to many and many to one relations in json-ld. For example :

{
  "@context" : {
    "@vocab" : "http://www.schema.org/",
    "@id" : "http://www.example.com/users/Joe",
    "name" : "name",
    "dob" : "birthDate",
    "age" : {
       "@id" : "http://www.example.com/users/Joe#age",
       "@type" : "Number"
        }
    "knows" : ["http://www.example.com/users/Jill", "http://www.example.com/users/James"]
    },
   "name" : "Joe",
   "age" : "24",
   "dob" : "12-Jun-2013"
}

this doesn't parse in json-ld playground. What is the valid and best way to specify relations like this either in json-ld or using Hydra?

1

There are 1 answers

3
Markus Lanthaler On BEST ANSWER

You need to be carful what you put into the context and what you put into the body of the document. Simply speaking the context defines the mappings to URLs while the body contains the actual data. Your example should thus look something like this:

{
  "@context" : {
    "@vocab" : "http://www.schema.org/",
    "dob" : "birthDate",
    "age" : {
      "@id" : "http://www.example.com/users/Joe#age",
      "@type" : "Number"
    },
    "knows": { "@type": "@id" }
  },
  "@id" : "http://www.example.com/users/Joe",
  "name" : "Joe",
  "age" : "24",
  "dob" : "12-Jun-2013",
  "knows" : [
    "http://www.example.com/users/Jill",
    "http://www.example.com/users/James"
  ]
}