Skip to content

Creating Swagger JSON from YAML files

Tony Tam edited this page Dec 21, 2013 · 3 revisions

If you're hand-crafting Swagger JSON files, you might want to use something more human-friendly. Since YAML converts to JSON pretty easily, you can do this with a simple conversion script, called yaml2swagger.

Steps to use:

# install dependencies from package.json
npm install

# make an output directory to write files into.  If not supplied, writes to cwd
mkdir output

# run the script against some sample yml files.  Arg[0] is the file/directory to convert, Arg[1] is the output dir
node bin/yml2swagger.js samples/yaml output

converting samples/yaml/api-docs.yml
converting samples/yaml/pet.yml
converting samples/yaml/store.yml
converting samples/yaml/user.yml
writing to output/api-docs.yml.json
writing to output/pet.yml.json
writing to output/store.yml.json
writing to output/user.yml.json

Now you have converted from yaml to json format, and can use all the swagger-codegen tools against them.

The yaml structure is very simple, and mirrors the json structure used with swagger. See these examples:

Resource Listing

api-docs.yml

Pet Api Declaration

pet.yml

Store Api Declaration

store.yml

User Api Declaration

user.yml