Skip to content

Instantly share code, notes, and snippets.

@imochoa
Last active September 22, 2022 09:49
Show Gist options
  • Save imochoa/78b407c2c689de4359b04ffdb5eef308 to your computer and use it in GitHub Desktop.
Save imochoa/78b407c2c689de4359b04ffdb5eef308 to your computer and use it in GitHub Desktop.
json-schema-viewer

JSON Schema viewer

Using json-schema-viewer to explore schemas

If you already have schemas, that's great. If you have OpenAPI specs instead of dealing with schemas you can follow the next section

(Optional) Get your schemas from OpenAPIv3 Specifications

A) Reference OpenAPI Specification

Let's say you start with something like this:

info:
  description: Some info
  title: your title
  version: 2.0.0
paths:
  /json:
    get:
      responses:
        default:
          description: 'OK response'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/OKResponse'
        '500':
          description: '500 response'
          content:
            application/json:
              schema:
                $ref: '#/components/schemas/FailResponse'
openapi: 3.0.2
components:
  schemas:
    OKResponse:
      type: object
      properties:
        size:
          type: number
        accuracy:
          type: string
        quantity:
          type: integer
    FailResponse:
      type: object
      properties:
        size:
          type: number
        accuracy:
          type: string
        quantity:
          type: integer

B) Extract the schemas

You can extract the schemas to schemas/ by running:

pipx run --spec git+https://github.com/imochoa/openapi2jsonschema openapi2jsonschema /path/to/your/spec.yaml -o schemas/

requires pipx

Prepare the schemas for the json-schema-viewer docker image

The docker image expects a schema.json at /var/www/schemas/schema/schema.json that must contain these 2 keys:

{
"version":"x.y.z",
"id": "schema.json#",
}

So locate your top-level schema and prepare it:

cd schemas
cp OKResponse.json schema.json
jq '. += {"version":"1.1.3"}' schema.json | sponge schema.json
jq '. += {"id": "schema.json#"}' schema.json | sponge schema.json

requires more-utils jq

Running the docker image

You have to mount your schemas onto /var/www/schemas/schema (including the schema.json file). Which you can do like so:

docker run --rm -it -p 9001:9001 -v `realpath schemas`:/var/www/schemas/schema imochoa/json-schema-viewer

For more info, see the dockerhub page

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment