Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting a Swagger YAML file to JSON from the command line

I'd like to convert a Swagger YAML file to JSON from the command line. The plan is to use this command line during a CI job. I've searched on google and found many solutions, but most of them use Python or Ruby, which I'd prefer not to use. For example: http://www.commandlinefu.com/commands/view/12218/convert-yaml-to-json

I'd like to do this without using Python or Ruby, and I'd also like to be able to control the leading whitespace when formatting the JSON to match exactly the JSON that is output from Swagger's editor.swagger.io editor, when you choose File -> Download JSON

All this means is that I'd like the whitespace padding to be four spaces, like so:

{     "swagger": "2.0",     "info": {         "title": "API TITLE", 

I haven't tried the Python method in the link above, but the Ruby method uses two space whitespace padding. Perhaps there is a way to control that, but I don't want to use Ruby or Python in this solution anyway.

I'm sure that there are many "correct" answers to this question. I am looking for the solution that is most elegant with the least number of dependencies. Ideally, a diff of the resulting JSON file against a JSON file generated by the editor.swagger.io should be empty.

like image 556
mkrufky Avatar asked Jan 11 '16 23:01

mkrufky


People also ask

Can you convert YAML to JSON?

Using PowerShell to Convert YAML to JSON. PowerShell is capable of modifying text files and converting objects to several formats in including JSON. Because PowerShell is extensible, you can use PowerShell to convert YAML to JSON format with the right module.

How do I export swagger JSON YAML file from Swagger UI?

If you don't see the url or if url is a code expression, open the browser dev tools, switch to the Network tab and disable caching. Then refresh the page and search for the API definition file ( swagger. json , swagger. yaml , api-docs or similar) among HTTP requests.

How do I convert swagger to YAML?

Go to http://editor.swagger.io/#/ On the top left corner, select File-> Import File... Point to the local Swagger JSON file you exported in step #1 to open in the Swagger Editor. Select Generate Client -> Swagger YAML option from the menu.


2 Answers

I think that you are looking for the swagger-codegen (now OpenApi-generator) functionality:

Running

swagger-codegen generate -i swagger.yaml -l swagger

will out put a swagger.json in the same location.

Update For CI: If you can install it on your build machine- good for you. If you can't - the github page has a link to a docker image with a nodejs server is available (to convert using a curl command as suggested in a different answer).

Update For Docker: If you use Docker, try swaggerapi/swagger-codegen-cli, there is an example for docker-compose that might help a few answers down by Fabian & ckeeney.

Update about OpenApi:

This question is about swagger, and a few years old. If you're just starting to use Swagger you should switch to OpenApi instead, and if you have existing swagger files, i suggest migrating.

like image 81
Liel Avatar answered Sep 29 '22 22:09

Liel


Using yamljs:

yaml2json swagger.yaml -p -i4 

The output from this command diff'd against the JSON output from editor.swagger.io produces an empty diff.

This is indeed what I'm looking for, but it brings in a huge dependency (node). I'm hoping for something even lighter, yet equally as elegant as this.

like image 24
mkrufky Avatar answered Sep 30 '22 00:09

mkrufky