Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save expobrain/ccf4955b37130c254deeddc002cc1f34 to your computer and use it in GitHub Desktop.
Save expobrain/ccf4955b37130c254deeddc002cc1f34 to your computer and use it in GitHub Desktop.
# From https://apihandyman.io/api-toolbox-jq-and-openapi-part-1-using-jq-to-extract-data-from-openapi-files/#list-operations
# 1 - Selects paths objects
#--------------------------
# returns [{key: path, value: path value}]
.paths # Selects the paths property content
| to_entries # Transforms
# { "/resources": { "get": {operation data}}}
# to
# [ { "key": "/resources",
# "value": { "get": {operation data}} ]
| map(select(.key | test("^x-") | not)) # Gets rid of x-tensions
# 2 - Creates an array of operations
#-----------------------------------
# returns [{path, method, tags, deprecated}]
| map ( # Applies a transformation to each element
.key as $path # Stores the path value (.key)
# in a variable ($path) for later use
| .value # Keeps only the path's content
# { "get": {operation data}}
| to_entries # Transforms
# { "get": {operation data}}
# to
# [ { "key": "get",
# "value": {operation data}} ]
| map( # Applies a transformation to each element
select( # Keeps only elements for which the following is true
# With IN, which returns true if the value is one of its
# parameters, we can get rid of x- , parameters
# description and tags properties
.key | IN("get", "put", "post", "delete",
"options", "head", "patch", "trace")
)
| # Creates a new JSON object
{
method: .key | ascii_upcase,
path: $path, # Using the variable defined on line 4
tags: .value.tags | tostring,
}
)[] # Flattens array to avoid having an array
# of array of {path, method, tags, deprecated}
) # Now we have an array of {path, method, tags, deprecated}
# 3 - Outputs tab separated raw text
#-----------------------------------
| map( # Applies a transformation to each element
.method + "\t" +
.path + "\t" +
.tags
)
[] # Flattens array for raw output
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment