This is the basic learning for JSON schema.
It is used for creating JSON easier like having an intellisense with guide and validations.
- Open vscode
- Create
schemas
folder - Create
your-filename.schema.json
and open it - Tell
vscode
about your JSON schemas. Go here - Write
{ }
- Press
Ctrl + SPACE
and input or select"$schema"
- Press again
Ctrl + SPACE
and choose a specification (i.e. http://json-schema.org/schema)
You can now define your own schema! See examples below...
schemas/profile.schema.json
{
"$schema": "http://json-schema.org/schema",
"$id": "profile.schema.json",
"required": ["name", "age"],
"properties": {
"name": {
"description": "Give details about your name.",
"$ref": "./name.schema.json"
},
"hobbies": {
"description": "Things you love to do on your free or \"me\" time.",
"$ref": "#/definitions/hobbiesdef"
},
"age": {
"description": "How old are you?",
"$ref": "#/definitions/agedef"
}
},
"definitions": {
"hobbiesdef": {
"$comment": "definitions are sub-schemas, and called in $ref, you can also create another schema file rather than having a definition",
"type": "array",
"uniqueItems": true,
"items": {
"type": "string",
"examples": [
"swimming",
"sleeping",
"eating",
"watching movies",
"playing music instruments",
"listening to music",
"playing outdoor games",
"playing video games",
"games"
]
},
"examples": [
["swimming", "doing laundry"],
["playing games", "while eating"]
]
},
"agedef": {
"type": "integer",
"exclusiveMinimum": 0
}
}
}
schemas/name.schema.json
{
"$schema": "http://json-schema.org/schema",
"$id": "name.schema.json",
"required": ["firstName", "lastName"],
"additionalProperties": false,
"properties": {
"firstName": {
"description": "Your first name goes here.",
"type": "string"
},
"lastName": {
"description": "Your surname goes here.",
"type": "string"
},
"nickname": {
"description": "Enter here what you want your close friends to call you.",
"type": "string"
},
"prefix": {
"description": "How you are addressed.",
"type": "string",
"enum": ["Mr.", "Ms.", "Dr.", "Sir", "Madam"]
},
"suffix": {
"description": "Your suffix (if any).",
"type": "string",
"examples": ["Jr", "Sr"]
}
}
}
profile.model.json
{
"name": {
"firstName": "Juan",
"lastName": "De La Cruz",
"suffix": "III"
},
"hobbies": [
"playing games",
"while eating",
"listening to music",
"listening to IU music"
],
"age": 3
}
You need to setup and tell vscode about your schema and where to apply it.
So that the intellisense, validation will display every time you write your JSON object/model.
- Press
Ctrl + P
- Type
>settings
- Select
Preferences: Open Workspace Settings (JSON)
- Validate that the following is created:
.vscode/settings.json
- You can now use its intellisense to construct the following:
.vscode/settings.json
{
"json.schemas": [
{
"fileMatch": ["*profile.model.json"],
"url": "./schemas/profile.schema.json"
}
]
}
-
Quicktype: https://github.com/quicktype/quicktype
This tool can create a schema out from a plain JSON
Also, it can create aTypescript Interface
straight from the schema!
npm install -g quicktype
The recommended way to use quicktype
is to generate a JSON schema from sample data, review and edit the schema, commit the schema to your project repo, then generate code from the schema as part of your build process:
# First, infer a JSON schema from a sample.
quicktype pokedex.json -l schema -o schema.json
# Review the schema, make changes,
# and commit it to your project repo.
# Finally, generate model code from schema in your
# build process for whatever languages you need:
quicktype -s schema schema.json -o src/ios/models.swift
quicktype -s schema schema.json -o src/android/Models.java
quicktype -s schema schema.json -o src/nodejs/Models.ts
# All of these models will serialize to and from the same
# JSON, so different programs in your stack can communicate
# seamlessly.