Swagger/OpenAPI: Mixed topics


Swagger file format

Converting Swagger YAML file to JSON format

You might have noticed that most of examples of swagger files out there in YAML format, but you might require a swagger file in JSON format, maybe because you are using a tool or an example which is using swagger.json format. So knowing how to convert a swagger YAML file to JSON format might be usefull for you.

This can be achieved with the tool openapi-generator, which can be installed in different ways (as docker, as npm package, …). Here is an example to install it in your local project folder using npm:

npm install @openapitools/openapi-generator-cli -D

Now you can test this tool, but first you need a valid swagger.yaml file to be sure that the yaml file you try to convert is valid for this tool. To be sure you can start the example provided by the creators of the tool:

https://openapi-generator.tech/docs/installation/

On the page above you can find a yaml example called petstore.yaml

You can use that YAML file to generate a corresponding swagger/openapi JSON file with the following command:

npx @openapitools/openapi-generator-cli generate -i example.yaml -g openapi -o example

This generates a folder called example and inside that folder you will find the convred openapi.json file.