Search code examples
json-ld

What is the proper way to reference another resource in JSON-LD


So I'm trying to wrap my head around JSON-LD, and all the examples I see mainly consist of embedding "linked data". But I want to provide references to linked data (mainly because embedding it all could result in 10MB payloads). So I'm wondering if I'm doing this right.

Here's what I have:

{
  "@context": "/contexts/Customers.jsonld",
  "@id": "/customers/1",
  "@type": "Customer",
  "sessions": {
    "@id": "/customers/1/sessions",
    "@type": "Session"
  },
  "dealer": "/dealers/2",
  "givenName": "Abe",
  "familyName": "Abrahms",
  /* ... snip ... */
}

The reference to linked data that I'm talking about here is represented by the sessions property. Assuming that is correct, then what would I need to change in my Customer context?

"@context": {
    "hydra": "http://www.w3.org/ns/hydra/core#",
    "doc": "https://api.waterlinkconnect.com/doc#",
    "Customer": "doc:Customer",
    "givenName": "doc:Customer/givenName",
    "familyName": "doc:Customer/familyName",
    "email":"doc:Customer/email",
    "address":"doc:Customer/address",
    "notes":"doc:Customer/notes",
    "phone1":"doc:Customer/phone1",
    "phone2":"doc:Customer/phone2"
    "sessions": "???????"
}

Solution

  • You just have to provide an IRI as value, e.g.:

    "propertyFoo": { "@id": "https://example.com/some-iri" }
    

    (@id is used here so that the IRI does not get interpreted as string value.)

    So your example with sessions is fine, but you don’t have to provide a @type if you don’t want/need it.

    With type coercion

    If this property always gets an IRI as value, you could define this in your @context:

    "propertyFoo": 
    { 
      "@id": "https://your-vocabulary.example.com/propertyFoo",
      "@type": "@id"
    }
    

    Then you can omit the @id when providing a value:

    "propertyFoo": "https://example.com/some-iri" 
    

    (If using type coercion like this, you can’t provide additional properties for that node.)