in swagger-codegen javascript bash json prettyprint ~ read.

How to pretty print JSON in bash console

Recently i was playing with Swagger Codegen tool and i needed to look at available configuration fields for java language. You can do it easily by running :

curl -X GET --header 'Accept: application/json' 'https://generator.swagger.io/api/gen/clients/java'

But, as an output, you'll get this :

{"sortParamsByRequiredFlag":{"opt":"sortParamsByRequiredFlag","description":"Sort method arguments to place required parameters before optional parameters.","type":"boolean","default":"true"},"ensureUniqueParams":{"opt":"ensureUniqueParams","description":"Whether to ensure parameter names are unique in an operation (rename parameters that are not).","type":"boolean","default":"true"}...

which is not readable at all. You would have to paste it into your favorite editor and then use prettyprint plugin there for JSON.
Surpisingly, i managed to find an easy and simple way to fix that. You can just call :

curl -X GET --header 'Accept: application/json' 'https://generator.swagger.io/api/gen/clients/java' | python -m json.tool

And that's it! The result will be :

{
    "sortParamsByRequiredFlag": {
        "opt": "sortParamsByRequiredFlag",
        "description": "Sort method arguments to place required parameters before optional parameters.",
        "type": "boolean",
        "default": "true"
    },
    "ensureUniqueParams": {
        "opt": "ensureUniqueParams",
        "description": "Whether to ensure parameter names are unique in an operation (rename parameters that are not).",
        "type": "boolean",
        "default": "true"
    },
    "allowUnicodeIdentifiers": {
        "opt": "allowUnicodeIdentifiers",
        "description": "boolean, toggles whether unicode identifiers are allowed in names or not, default is false",
        "type": "boolean",
        "default": "false"
    },..

You don't have to install anything - it just works out of the box. ( Tested on Maco OS). You can add to your .aliases file this command :

alias prettyjson='python -m json.tool'

And now you can use it as :

curl -X GET --header 'Accept: application/json' 'https://generator.swagger.io/api/gen/clients/java' | prettyjson

Of course, you can also have some functions created for it, but for now this is good enough for me.

Happy coding :)

comments powered by Disqus
comments powered by Disqus