Skip to content
Harold Solbrig edited this page Feb 23, 2016 · 3 revisions

Converting JSON Schema to JSON-LD

bioCADDIE Working Group 3 is developing a set of JSON Schema definitions. Our goal is to use this tag suite to extract as much information as is possible for the purpose of transforming external metadata into bioCaddie compatible RDF.

The schema_to_context script converts JSON Schema(s) to JSON-LD contexts.

Installing schema_to_context

  1. Make sure you have a running image of python 3
  2. Enter the appropriate virtual environment
> . myenv/bin/activate
(myenv) >

3a. Install schema_to_context from github

(myenv) > git clone https://github.com/crDDI/schema_to_context
(myenv) > cd schema_to_context
(myenv) > python setup.py install

3b. Install schema_to_context from PyPi

(myenv) > pip install schema_to_context

4 . Run schema_to_context

(myenv) > schema_to_context
usage: schema_to_context [-h] [-i [INFILE [INFILE ...]]] [-id INDIR]
                         [-o [OUTFILE [OUTFILE ...]]] [-od OUTDIR] [-f] [-s]
                         -n NAMESPACE -u NSURI [--logfile LOGFILE]
                         [--loglevel {DEBUG,INFO,WARNING,ERROR}]
schema_to_context: error: the following arguments are required: -n/--namespace, -u/--nsuri

Running schema_to_context

To convert a single JSON Schema file:

(myenv)> schema_to_context -i json-schemas/identifier_info_schema.json -o json-ld/identifier_info.jsonld -n ex -u http://www.example.org/id#
2016-02-23 12:34:55,366  INFO - PROCESSING: json-schemas/identifier_info_schema.json
2016-02-23 12:34:55,367  INFO - Total files processed=1 Errors=0

This example would take:

{
    "$schema": "http://json-schema.org/draft-04/schema",
    "title": "ISA identifier information schema",
    "description": "JSON-schema representing an identifier and its scheme in the ISA model v2.0",
    "type": "object",
    "properties": {
        "identifier": {
           "description": "a code uniquely identifying the dataset",
           "type" : "string",
           "format": "uri"
        },
        "identifierScheme": {
            "description": "if an identifier is supplied, the identifier scheme represent information about how identifiers are formed, maintained and minted",
            "type" : "string"
        }
    }
}

and produce:

{
   "@context": [
      {
         "ex": "http://www.example.org/id#",
         "identifierScheme": {
            "@id": "ex:identifierScheme"
         },
         "identifier": {
            "@id": "ex:identifier",
            "@type": "@id"
         }
      }
   ]
}

To convert set of json schema files

(myenv) > schema_to_context -id json-schemas -od json-ld --logfile conv.log -n ex -u http://www.example.org/id# -a dbgap_schema.json

The above converts all of the files that end with ".json" in the json-schemas directory and places the results in the json-ld directory. This will also write a context.json which lists all of the converted files:

{
   "@context": [
      "disease_schema.json",
      "biological_process_schema.json",
      ...
      "dimension_schema.json",
      "dbgap_schema.json"
   ]
}
(myenv) >schema_to_context -h
usage: schema_to_context [-h] [-i [INFILE [INFILE ...]]] [-id INDIR]
                         [-o [OUTFILE [OUTFILE ...]]] [-od OUTDIR] [-f] [-s]
                         -n NAMESPACE -u NSURI
                         [-a [ADDLFILES [ADDLFILES ...]]] [--logfile LOGFILE]
                         [--loglevel {DEBUG,INFO,WARNING,ERROR}]

Convert json schema(s) to jsonld context(s)

optional arguments:
  -h, --help            show this help message and exit
  -i [INFILE [INFILE ...]], --infile [INFILE [INFILE ...]]
                        Input file(s)
  -id INDIR, --indir INDIR
                        Input directory
  -o [OUTFILE [OUTFILE ...]], --outfile [OUTFILE [OUTFILE ...]]
                        Output file(s)
  -od OUTDIR, --outdir OUTDIR
                        Output directory
  -f, --flatten         Flatten output directory
  -s, --stoponerror     Stop on processing error
  -n NAMESPACE, --namespace NAMESPACE
                        Default namespace identifier
  -u NSURI, --nsuri NSURI
                        URI for default namespace
  -a [ADDLFILES [ADDLFILES ...]], --addlfiles [ADDLFILES [ADDLFILES ...]]
                        Additional context file(s) to add to context.json
  --logfile LOGFILE     Log file. Default is stdout
  --loglevel {DEBUG,INFO,WARNING,ERROR}
                        Logging level. Default is INFO

Clone this wiki locally