'Extracting objects attributes from Swagger JSON schema using jq

I want to extract objects name and their attributes (.properties) from .definitions part of swagger json file, like:

{
  "definitions": {
    "Pet": {
      "type": "object",
      "required": ["pet_type"],
      "properties": {
        "pet_type": {
          "type": "string"
        }
      },
      "discriminator": {
        "propertyName": "pet_type"
      }
    },
    "Dog": {
      "allOf": [{
          "$ref": "#/components/schemas/Pet"
        }, {
          "type": "object",
          "properties": {
            "bark": {
              "type": "boolean"
            },
            "breed": {
              "type": "string",
              "enum": ["Dingo", "Husky", "Retriever", "Shepherd"]
            }
          }
        }
      ]
    }
  }
}

to:

object_name:
  attribute_name

like:

Pet:
  pet_type
Dog:
  bark
  breed

The point is .properties sometimes come directly after object name and sometimes as part of allOf, anyOf, or oneOf



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source