Translating between schema using JSON-LD

Carl Boettiger



JSON-LD transforms: Expansion and Compaction

One of the central motivations of JSON-LD is making it easy to translate between different representations of what are fundamentally the same data types. Doing so uses the two core algorithms of JSON-LD: expansion and compaction, as this excellent short video by JSON-LD creator Manu Sporny describes.

Here’s how we would use JSON-LD (from R) to translate between the two examples of JSON data from different providers as shown in the video. First, the JSON from the original provider:

ex <-
  "shouter": "",
  "txt": ""
"shouter": "Jim",
"txt": "Hello World!"

Next, we need the context of the second data provider. This will let us translate the JSON format used by provider one (“Shouttr”) to the second (“BigHash”):

bighash_context <- 
  "user": "",
  "comment": ""

With this in place, we simply expand the original JSON and then compact using the new context:

jsonld_expand(ex) %>%
  jsonld_compact(context = bighash_context)
  "@context": {
    "user": "",
    "comment": ""
  "comment": "Hello World!",
  "user": "Jim"


The CodeMeta Crosswalk table seeks to accomplish a very similar goal. The crosswalk table provides a human-readable mapping of different software metadata providers into the codemeta context (an extension of For instance, we’ll read in some data from GitHub:


Here we crosswalk the JSON data returned as the repository information from the GitHub API:

repo_info <- gh::gh("/repos/:owner/:repo", owner = "ropensci", repo = "EML")

Let’s just take a look at what the returned json data looks like:

repo_info %>% toJSON()
{"id":[10894022],"name":["EML"],"full_name":["ropensci/EML"],"owner":{"login":["ropensci"],"id":[1200269],"avatar_url":[""],"gravatar_id":[""],"url":[""],"html_url":[""],"followers_url":[""],"following_url":["{/other_user}"],"gists_url":["{/gist_id}"],"starred_url":["{/owner}{/repo}"],"subscriptions_url":[""],"organizations_url":[""],"repos_url":[""],"events_url":["{/privacy}"],"received_events_url":[""],"type":["Organization"],"site_admin":[false]},"private":[false],"html_url":[""],"description":[" Ecological Metadata Language interface for R: synthesis and integration of heterogenous data"],"fork":[false],"url":[""],"forks_url":[""],"keys_url":["{/key_id}"],"collaborators_url":["{/collaborator}"],"teams_url":[""],"hooks_url":[""],"issue_events_url":["{/number}"],"events_url":[""],"assignees_url":["{/user}"],"branches_url":["{/branch}"],"tags_url":[""],"blobs_url":["{/sha}"],"git_tags_url":["{/sha}"],"git_refs_url":["{/sha}"],"trees_url":["{/sha}"],"statuses_url":["{sha}"],"languages_url":[""],"stargazers_url":[""],"contributors_url":[""],"subscribers_url":[""],"subscription_url":[""],"commits_url":["{/sha}"],"git_commits_url":["{/sha}"],"comments_url":["{/number}"],"issue_comment_url":["{/number}"],"contents_url":["{+path}"],"compare_url":["{base}...{head}"],"merges_url":[""],"archive_url":["{archive_format}{/ref}"],"downloads_url":[""],"issues_url":["{/number}"],"pulls_url":["{/number}"],"milestones_url":["{/number}"],"notifications_url":["{?since,all,participating}"],"labels_url":["{/name}"],"releases_url":["{/id}"],"deployments_url":[""],"created_at":["2013-06-23T23:20:03Z"],"updated_at":["2017-05-11T21:24:40Z"],"pushed_at":["2017-07-05T18:52:34Z"],"git_url":["git://"],"ssh_url":[""],"clone_url":[""],"svn_url":[""],"homepage":[""],"size":[5094],"stargazers_count":[48],"watchers_count":[48],"language":["HTML"],"has_issues":[true],"has_projects":[true],"has_downloads":[true],"has_wiki":[true],"has_pages":[true],"forks_count":[17],"mirror_url":{},"open_issues_count":[35],"forks":[17],"open_issues":[35],"watchers":[48],"default_branch":["master"],"organization":{"login":["ropensci"],"id":[1200269],"avatar_url":[""],"gravatar_id":[""],"url":[""],"html_url":[""],"followers_url":[""],"following_url":["{/other_user}"],"gists_url":["{/gist_id}"],"starred_url":["{/owner}{/repo}"],"subscriptions_url":[""],"organizations_url":[""],"repos_url":[""],"events_url":["{/privacy}"],"received_events_url":[""],"type":["Organization"],"site_admin":[false]},"network_count":[17],"subscribers_count":[18]} 

We can crosswalk this information into codemeta just by supplying the column name to the crosswalk function. This performs the same expansion of the metadata in the GitHub context, followed by compaction into the codemeta context. Note that terms not recognized/included in the codemeta context will be dropped:

github_meta <- crosswalk(repo_info, "GitHub")
  "@context": "",
  "codeRepository": "",
  "dateCreated": "2013-06-23T23:20:03Z",
  "dateModified": "2017-05-11T21:24:40Z",
  "description": " Ecological Metadata Language interface for R: synthesis and integration of heterogenous data",
  "downloadUrl": "{archive_format}{/ref}",
  "identifier": "10894022",
  "name": "ropensci/EML",
  "programmingLanguage": "",
  "issueTracker": "{/number}"

We can verify that the result is a valid codemeta document:

[1] TRUE

Transforming into other column schema

The above transform showed the process of going from plain JSON data into the codemeta standard serialization. Similarly, we can crosswalk into any of the other columns in the crosswalk table. To do so, the crosswalk function will first expand any of the recognized properties into the codemeta JSON-LD context, just as above. Unrecognized properties are dropped, since there is no consensus context into which we can expand them. Second, the expanded terms are then compacted down into the new context (Zenodo in this case.) This time, any terms that are not part of the codemeta context are kept, but not compacted, since they still have meaningful contexts (that is, full URIs, e.g. URLs) that can be associated with them:

crosswalk(repo_info, "GitHub", "Zenodo") %>%
  "relatedLink": "",
  "schema:dateCreated": {
    "@type": "schema:Date",
    "@value": "2013-06-23T23:20:03Z"
  "schema:dateModified": {
    "@type": "schema:Date",
    "@value": "2017-05-11T21:24:40Z"
  "description/notes": " Ecological Metadata Language interface for R: synthesis and integration of heterogenous data",
  "schema:downloadUrl": {
    "@id": "{archive_format}{/ref}"
  "id": "10894022",
  "name": "ropensci/EML",
  "schema:programmingLanguage": "",
  "codemeta:issueTracker": {
    "@id": "{/number}"

Thus terms that still have a uncompacted prefix like schema: or codemeta: reflect properties that we could successfully extract from the input data, but do not have corresponding properties in the Zenodo context. This is the standard behavior of the compaction algorithm: unrecognized fields are not dropped, but are also not compacted, making them accessible only if referenced explicitly.

NodeJS example

NodeJS uses a package.json format that is very similar to a simple codemeta.json file, though it is not Linked Data as it does not declare a context. Here we crosswalk an example package.json file into proper codemeta standard.

package.json <- read_json(
[1] "node-js-sample"

[1] "0.2.0"

[1] "A sample Node.js app using Express 4"

[1] "index.js"

[1] "node index.js"

[1] "^4.13.3"

[1] "4.0.0"

[1] "git"

[1] ""

[1] "node"

[1] "heroku"

[1] "express"

[1] "Mark Pundsack"

[1] "Zeke Sikelianos <> ("

[1] "MIT"
crosswalk(package.json, "NodeJS")
  "@context": "",
  "codeRepository": {},
  "creator": "Mark Pundsack",
  "description": "A sample Node.js app using Express 4",
  "keywords": [
  "license": "MIT",
  "name": "node-js-sample",
  "version": "0.2.0"

Note that while nested structures per se pose no special problem, the compaction/expansion paradigm lacks a mechanism to capture differences in nesting between schema. For instance, in codemeta (i.e. in, a codeRepository is expected to be a URL, while NodeJS package.json permits it to be another object node with sub-properties type and url. There is no way in JSON-LD transforms or context definitions to indicate that the url sub-property in the NodeJS case, e.g. codeRepository.url maps to schema’s codeRepository. (This same limitation is also true of the 2-dimensional table structure of the crosswalk itself, though it is important to keep in mind that this 1:1 mapping requirement is not unique to the the .csv representation but also inherent in JSON-LD contexts.)

Consequently, a thorough translation between formats that do not provide there own JSON-LD contexts will ultimately require more manual implementation, which would be expressed within a particular programming language (e.g. R) rather than in the generic algorithms of JSON-LD available in many common programming languages.