简体   繁体   中英

Adding custom properties to an existing type in JSON-LD

I'm investigating the use of JSON-LD and Hydra for use as an API payload. It seems to be very promising, but I'm struggling with one thing - how to extend pre-defined types.

For example, I have users. It seems reasonable to model them as "@type": "http://schema.org/Person" - since users are people. But I also want to have some properties on my users that are not defined on that type - eg user account information.

I know that I can give a resource multiple types, so I can define my own vocabulary that defines a type of "User" having this information, but is that the correct way to achieve this? Or is there a better way using JSON-LD to achieve this goal?

In general, there are four ways how it can be approached:

  • For types (aka. classes):
    • Add a type. To convey that a schema:Person is fictional, you could add a ex:FictionalThing type to that entity.
    • Use a sub-type instead. To convey that a schema:Person is fictional, you could define ex:FictionalPerson to be a sub-type of schema:Person , and only¹ use ex:FictionalPerson .
  • For properties:
    • Add a property. To provide a nickname for a schema:Person , you could add a ex:nickname property.²
    • Use a sub-property instead. You could define ex:nickname to be a sub-property of schema:additionalName , and only¹ use ex:nickname

It's a good practice to reuse existing RDF types/properties . If there is no suitable term/vocabulary, define your own .


In your specific case, it seems to me that you need a type for representing a user account, as a schema:Person could have multiple accounts, and there is probably no user data that is true for all user accounts of that person. So you might want to consider using an entity for representing a user account, and a property to connect such an account with a schema:Person .

You could, for example, use the property foaf:account to add a foaf:OnlineAccount to the schema:Person :

{
  "@context": "http://www.w3.org/2013/json-ld-context/rdfa11",

  "@type": "schema:Person",
  "schema:name": "Name of the person",

  "foaf:account": {
    "@type": "foaf:OnlineAccount",
    "schema:name": "Name of the online account"
  }
}

¹ Or use it in addition to the super-type/super-property, which is typically a good idea if you want to support consumers who don't read your own vocabulary first.

² You don't have to add another type next to schema:Person for this purpose, as you can freely mix types and properties from different vocabularies. Also note that you can define the domain and range of the property, which allows you to "implicitly" add types to the referencing/referenced entities. In the example above, a consumer who knows the FOAF vocabulary will add the type foaf:Agent to the entitiy with the type schema:Person , because it's the domain of the property foaf:account .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM