Skip to content
46 changes: 46 additions & 0 deletions docs/tutorials/python/schema_operations.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
JSON Schema is a tool used to validate data. In Synapse, JSON Schemas can be used to validate the metadata applied to an entity such as project, file, folder, table, or view, including the [annotations](https://help.synapse.org/docs/Annotating-Data-With-Metadata.2667708522.html) applied to it. To learn more about JSON Schemas, check out [JSON-Schema.org](https://json-schema.org/).

Synapse supports a subset of features from [json-schema-draft-07](https://json-schema.org/draft-07). To see the list of features currently supported, see the [JSON Schema object definition](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/schema/JsonSchema.html) from Synapse's REST API Documentation.

In this tutorial, you will learn how to create these JSON Schema using an existing data-model.

## Tutorial Purpose
You will create a JSON schema using your data model.

## Prerequisites
* You have a working [installation](../installation.md) of the Synapse Python Client.
* You have a data-model, see [here](https://sagebionetworks.jira.com/wiki/spaces/SCHEM/pages/2473623559/The+Data+Model+Schema)


## 1. Set up your variables

```python
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=10-14}
```

To create a JSON Schema you need a data-model, and the data-type you want to create. The data-model must be in either CSV or JSON-LD form. See [here](https://sagebionetworks.jira.com/wiki/spaces/SCHEM/pages/2473623559/The+Data+Model+Schema) for instructions on how to crate a data-model.The data-type must exist in your data-model.

## 2. Create the JSON Schema
Try creating the JSON Schema
```python
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!lines=16-24}
```
You should see the JSON Schema for the datatype you selected printed.


## Source Code for this Tutorial

<details class="quote">
<summary>Click to show me</summary>

```python
{!docs/tutorials/python/tutorial_scripts/schema_operations.py!}
```
</details>


## Reference
- [Data models](https://sagebionetworks.jira.com/wiki/spaces/SCHEM/pages/2473623559/The+Data+Model+Schema)
- [JSON Schema Object Definition](https://rest-docs.synapse.org/rest/org/sagebionetworks/repo/model/schema/JsonSchema.html)
- [JSON Schema Draft 7](https://json-schema.org/draft-07)
- [JSON-Schema.org](https://json-schema.org./)
24 changes: 24 additions & 0 deletions docs/tutorials/python/tutorial_scripts/schema_operations.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import logging

from synapseclient.extensions.curator.schema_generation import (
DataModelGraph,
DataModelGraphExplorer,
DataModelParser,
create_json_schema,
)

LOGGER = logging.Logger("create_json_schema")
# Put the path to your data model here, either CSV or JSONLD format
DATA_MODEL_PATH = "tests/unit/synapseclient/extensions/schema_files/example.model.csv"
# Put the name of the datatype in your data-model you want to create here
DATATYPE = "Patient"

data_model_parser = DataModelParser(path_to_data_model=DATA_MODEL_PATH, logger=LOGGER)
parsed_data_model = data_model_parser.parse_model()
data_model_grapher = DataModelGraph(parsed_data_model)
graph_data_model = data_model_grapher.graph
dmge = DataModelGraphExplorer(graph_data_model, logger=LOGGER)
json_schema = create_json_schema(
dmge, datatype=DATATYPE, schema_name=DATATYPE, logger=LOGGER
)
print(json_schema)
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ nav:
# - Team: tutorials/python/team.md
- Upload data in bulk: tutorials/python/upload_data_in_bulk.md
- Download data in bulk: tutorials/python/download_data_in_bulk.md
- Creating JSON Schema: tutorials/python/schema_operations.md
- Working with JSON Schema: tutorials/python/json_schema.md
# - Move Files and Folders: tutorials/python/move_files_and_folders.md
# - Migrate data to other storage locations: tutorials/python/migrate_data_to_other_storage_locations.md
Expand Down
Loading