Developers share a common problem: they want a simple, but extensible way to create an API for a web service that gets the job done, doesn't design them into a corner, and allows developers to easily interact with their service without reinventing the wheel. JSON-LD [[JSON-LD]] has become an important solution, as it bridges the gap between formally data and more colloquial JSON interfaces used in APIs from numerous providers. This guide attempts to define certain best practices for publishing data using JSON-LD, and interacting with such services.

Specification Moved

The Community Group completed work on this specification, the work is being continued by the JSON-LD Working group, see https://w3c.github.io/json-ld-bp/ for the Editor's draft.

This is an unofficial document of the Linking Data in JSON Community Group.

Introduction

Coming up with a data format for your API is a common problem. It can be hard to choose between different data representations, what names you want to pick and even harder if you want to leave room for extensibility. How do you make all these decisions? How do make your API easy to use so people can use short strings to reference common things, but URLs to enable people to come up with their own so it isn't limiting? How can you make it easy for other people to add their own data in and make it interoperable? How do you consume data from other similar apps? There are technologies that can help you do this. EXAMPLES. Now, it isn't perfect – sometimes it won't solve your problem, but it could maybe solve a lot of them.

The use of JSON on the web has grown immensely in the last decade, particularly with the explosion of APIs that eschew XML in favor of what is considered to be a more developer friendly format which is directly compatible with JavaScript. As a result, different sites have chosen their own proprietary representations for interacting with their sites, sometimes described using frameworks such as [[swagger]] which imply a particular URI composition for interacting with their services. This practice leads to vendor-specific semantic silos, where the meaning of a particular JSON document makes sense only by programming directly to the API documentation for a given service.

show examples from GitHub, Twitter, …?

As services grow the often introduce incompatible changes leading to a Version 2 or Version 3 of their API requiring developers to update client code to properly handle JSON documents. In many cases, even small changes can lead to incompatibilities. Additionally, composing information from multiple APIs becomes problematic, due to namespace or document format conventions that may differ between API endpoints. Moreover, the same principles are often repeated across different endpoints using arbitrary identifiers (name, email, website, etc.); the community needs to learn to stop repeating itself (DRY concept) and reuse common conventions, although this does not necessarily have to mean using exactly the same identifiers within the JSON itself (see JSON-LD Context).

This Note proposes to outline a number of best practices for API designers JSON developers based on the principles of separation of data model from syntax, the use of discoverable identifiers describing document contents, and general organizing principles that allow documents to be machine understandable (read, interpreted as JSON-LD using Linked Data, RDF and RDFS vocabulary and data model principles).

Key among these is the notion of vocabulary re-use, so that each endpoint does not need to separately describe the properties and structure of their JSON documents. Schema.org provides a great example of doing this, and includes an extension mechanism that may already be familiar to API designers.

This note expands on Data on the Web Best Practices [[dwbp]] and Best Practices for Publishing Linked Data [[ld-bp]].

Terminology

JSON-LD
JSON-LD [[JSON-LD]] is a lightweight Linked Data format. It is easy for humans to read and write. It is based on the already successful JSON format and provides a way to help JSON data interoperate at Web-scale. JSON-LD is an ideal data format for programming environments, REST Web services, and unstructured databases such as CouchDB and MongoDB.
JSON-LD Context
In JSON-LD, a context is used to map terms, i.e., properties with associated values in an JSON document, to URLs. A term is a short word that expands to a URL. Terms may be defined as any valid JSON string other than a JSON-LD keyword.
Linked Data
Linked Data [[linked-data]] is a way to create a network of standards-based machine interpretable data across different documents and Web sites. It allows an application to start at one piece of Linked Data, and follow embedded links to other pieces of Linked Data that are hosted on different sites across the Web.

Resource Representation

Publish data using developer friendly JSON

JSON [[json]] is the most popular format for publishing data through APIs; developers like it, it is easy to parse, and it is supported natively in most programming languages.

          {
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States"
          }
        

Use well-known identifiers when describing data

By sticking to basic JSON data expression, and providing a JSON-LD Context, all keys used within a JSON document can have unambigious meaning, as they bind to URLs which describe their meaning.

          {
            "@context": "http://schema.org",
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States"
          }
        

When expanding such a data representation, a JSON-LD processor replaces these terms with the URIs they expand to (as well as making property values unambiguous):

            [
              {
                "http://schema.org/familyName": [{"@value": "Obama"}],
                "http://schema.org/givenName": [{"@value": "Barack"}],
                "http://schema.org/jobTitle": [{"@value": "44th President of the United States"}],
                "http://schema.org/name": [{"@value": "Barack Obama"}]
              }
            ]
          

Expanded form is not useful as is, but is necessary for performing further algorithmic transformations of JSON-LD data and is useful when validating that JSON-LD entity descriptions say what the publisher means.

Cache JSON-LD Contexts

While most use of JSON-LD should not require a client to change the data representation, JSON-LD does allow the use of various algorithms to re-shape a JSON-LD document. These require the use of the JSON-LD Context, which is typically represented using a link to a remote document. Because it is remote, processing time can be severely impacted by the time it takes to retrieve this context. Services providing a JSON-LD Context SHOULD set HTTP cache-control headers to allow liberal caching of such contexts, and clients SHOULD attempt to use a locally cached version of these documents. Typically, libraries used to process JSON-LD documents should do this for you. (See also [[json-ld-best-practice-caching]]).

Use a top-level object

JSON documents may be in the form of a object, or an array of objects. For most purposes, developers need a single entry point, so the JSON SHOULD be in the form of a single top-level object

Use native values

When possible, property values SHOULD use native JSON datatypes such as numbers (integer, decimal and floating point) and booleans (true and false).

Assume arrays are unordered

JSON specifies that the values in an array are ordered, however in many cases arrays are also used for values which are unordered. Unless specified within the JSON-LD Context, multiple array values SHOULD be presumed to be unordered. (See Lists and Sets in [[JSON-LD]]).

Provide one or more types for JSON objects

Principles of Linked Data dictate that messages SHOULD be self describing, which includes adding a type to such messages.

Many APIs use JSON messages where the type of information being conveyed is inferred from the retrieval endpoint. For example, when retrieving information about a GitHub Commit, you might see the following response:

          {
            "sha": "7638417db6d59f3c431d3e1f261cc637155684cd",
            "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/7638417db6d59f3c431d3e1f261cc637155684cd",
            "author": {
              "date": "2014-11-07T22:01:45Z",
              "name": "Scott Chacon",
              "email": "schacon@gmail.com"
            },
            "committer": {
              "date": "2014-11-07T22:01:45Z",
              "name": "Scott Chacon",
              "email": "schacon@gmail.com"
            },
            "message": "added readme, because im a good github citizen\n",
            "tree": {
              "url": "https://api.github.com/repos/octocat/Hello-World/git/trees/691272480426f78a0138979dd3ce63b77f706feb",
              "sha": "691272480426f78a0138979dd3ce63b77f706feb"
            },
            "parents": [
              {
                "url": "https://api.github.com/repos/octocat/Hello-World/git/commits/1acc419d4d6a9ce985db7be48c6349a0475975b5",
                "sha": "1acc419d4d6a9ce985db7be48c6349a0475975b5"
              }
            ]
          }
        

The only way to know this is a commit is to infer it based on the published API documentation, and the fact that it was returned from an endpoint defined for retrieving information about commits.

          {
            "@context": "http://schema.org",
            "id": "http://www.wikidata.org/entity/Q76",
            "type": "Person",
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States"
          }
        

Identify objects with a unique identifier

Entities described in JSON objects often describe web resources having a URL; entity descriptions SHOULD use an identifier uniquely identifying that entity. In this case, using the resource location as the identity of the object is consistent with this practice.

          {
            "@context": "http://schema.org",
            "id": "http://www.wikidata.org/entity/Q76",
            "type": "Person",
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States"
          }
        

There is often ambiguous if an identifier is about the entity being described, or directly represents that entity. As an example, Barack Obama may have a Wikidata entry http://www.wikidata.org/entity/Q76, but it would be a mistake to say that http://www.wikidata.org/entity/Q76 is Barack Obama. However, it is common to use this pattern, particularly if the type of the entity describes a Person, rather than a WebPage.

Provide a representation of the entity related by URL

When dereferencing an entity related via a URL, the location SHOULD provide a representation of that entity. (This practices replicates that described in [[ld-bp]] Provide at least one machine-readable representation of the resource identified by the URI)

Corollaries to this best practice is that Cool URIs don't change [[cooluris]], meaning that URLs describing entities SHOULD be stable and not depend on variable information. Also, the URL used to identify an entity is the best API endpoint of that entity (see also ).

External references should use typed term

When using a property intended to reference another entity, properties SHOULD be defined to type string values as being references.

For example, the schema:image property a Thing to an Image:

          {
            "@context": "http://schema.org",
            "id": "http://www.wikidata.org/entity/Q76",
            "type": "Person",
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States",
            "image": "https://commons.wikimedia.org/wiki/File:President_Barack_Obama.jpg"
          }
        

This will be interpreted as a reference, rather than a string literal, because (at the time of publication), the schema.org JSON-LD Context defines image to be of type @id:

          {
            "@context": {
              ...
              "image": { "@id": "schema:image", "@type": "@id"},
              ...
            }
          }
        

If not defined as such in a remote context, terms may be (re-) defined in a local context:

          {
            "@context": ["http://schema.org", {
              "image": { "@id": "schema:image", "@type": "@id"}
            }],
            "id": "http://www.wikidata.org/entity/Q76",
            "type": "Person",
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States",
            "image": "https://commons.wikimedia.org/wiki/File:President_Barack_Obama.jpg"
          }
        

Nest referenced inline objects

When multiple related entity descriptions are provided inline, related entities SHOULD be nested.

For example, when relating one entity to another, where the related entity is described in the same message:

          {
            "@context": "http://schema.org",
            "id": "http://www.wikidata.org/entity/Q76",
            "type": "Person",
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States",
            "spouse": {
              "id": "http://www.wikidata.org/entity/Q13133",
              "type": "Person",
              "name": "Michelle Obama",
              "spouse": "http://www.wikidata.org/entity/Q76"
            }
          }
        

In this example, the spouse relationship is bi-directional, we have arbitrarily rooted the message with Barack Obama, and created a symetric relationship from Michelle back to Barack by reference, rather than by nesting.

Things not strings

When describing attributes, entity references SHOULD be used instead of string literals.

In some cases, when describing an attribute of an entity, it is tempting to using string values which have no independent meaning. Such values often are used for well known things. A JSON-LD context can define a term for such values, which allow them to appear as strings within the message, but be associated with specific identifiers. In this case, the property must be defined with type @vocab so that values will be interpreted relative to a vocabulary rather than the file location.

          {
            "@context": ["http://schema.org", {
              "gender": {"@id": "schema:gender", "@type": "@vocab"}
            }],
            "id": "http://www.wikidata.org/entity/Q76",
            "type": "Person",
            "name": "Barack Obama",
            "givenName": "Barack",
            "familyName": "Obama",
            "jobTitle": "44th President of the United States",
            "gender": "Male"
          }
        

See article in SEO Skeptic [[seo-strings-to-things]] for further elaboration on the advantages of using things instead of strings.

When describing an inverse relationship, use a referenced property

Serializing Large Collections

Describe schema.org extension using Role sub-class, Hydra collections, and LDP collections.

Reuse Vocabularies

Focus on schema.org?

Describe API affordances

Describe the use of schema.org Actions and work in Hydra.

Describe anti-pattern of URI construction emphasizing affordances.

API Versioning

Remember that Cool URIs don't change [cooluris]; correctly modeling data allows changes data representation to be limited.

Describe the use of API keys for controlling API versions, rather than the use of different versioned URLs.