Tool for converting a spreadsheet (a csv file) in a specific format to JSON Schema.
pip install spreadsheet-to-schema
The following will print out the JSON Schema generated by the CSV file:
spreadsheet-to-schema myfile.csv
To save to a file:
spreadsheet-to-schema myfile.csv > output.json
If you want merge the output schema to an existing JSON Schema file:
spreadsheet-to-schema myfile.csv -s myschema.json
The spreadsheet should look something like the following:
path | title | description | type |
---|---|---|---|
rootobj | my root description | object | |
p.myobject | myobject | my description | object |
p.mystring | mytitle2 | my description 2 | string |
p.myobject.p.substring | mysubstring | my substring description | string |
This will generate the following JSON Schema:
{
"type": "object",
"title": "rootobj",
"description": "my root description",
"properties": {
"mystring": {
"type": "string",
"title": "mytitle2",
"description": "my description 2"
},
"myobject": {
"type": "object",
"title": "myobject",
"description": "my description",
"properties": {
"substring": {
"type": "string",
"title": "mysubstring",
"description": "my substring description"
}
}
}
}
}
The path heading is required in the spreadsheet; all other headings are optional.
The path specifies where the JSON Schema object should exist and the other headings say what JSON Schema properties live at that path.
An empty path represents the root of the JSON Schema object. It will typically be of type object
and contain a properties
key.
The path is separated by .
separators. Each .
represents a new sub-object in the JSON.
There are some shortcuts to common parts of the path within JSON Schema:
p
can be used in place ofproperties
.d
can be used in place ofdefinitions
.i
can be used in place ofitems
.
Each column needs a heading. The following headings are supported:
path
: required, described abovetype
: type field in JSON Schema e.gobject
,string
,array
title
: title of the propertyformat
: format for the value of the propertydescription
: description of the propertycodelist
: name of codelist file (this is a nonstandard extension to JSON Schema)enum
: comma separated list of possible values for a fieldrequired
: comma separated list of required fieldsminItems
: the minimum number of items in an ArrayminProperties
: the minimum number of properties on an Objectref
: reference to another schema (a URI, for JSON Schema$ref
keyword)
- Any extra column headings in the CSV will be ignored, so could be used to add notes not intended to be in the schema.
- No validity checking is done on the output JSON Schema so care needs to be taken when defining the paths, to make sure they are valid.
- A good understanding of JSON Schema is needed to define the paths and fields correctly, some properties will not make sense at certain paths.